You are given two independent coding tasks.
Given the head of a singly linked list (not necessarily sorted), remove duplicate values so that each value appears only once, keeping the first occurrence of each value and preserving the original relative order of the remaining nodes.
head
of a singly linked list where each node has fields
val
and
next
.
n
up to
10^5
; node values fit in 32-bit signed int.
Example
1 -> 3 -> 1 -> 2 -> 3 -> null
1 -> 3 -> 2 -> null
Given two strings x and y, find one longest string s such that:
s
is a
subsequence
of
x
(characters chosen in order, not necessarily contiguous), and
s
is a
substring
of
y
(contiguous segment of
y
).
If multiple answers have the same maximum length, you may return any one of them.
x
,
y
s
meeting the conditions with maximum possible length (or an empty string if no non-empty solution exists).
1 ≤ |x|, |y| ≤ 2000
; characters are lowercase English letters.
Example
x = "abac"
,
y = "zzbaczz"
"bac"
(it is a subsequence of
x
and a substring of
y
).