You are given two independent coding tasks.
Task 1: Remove duplicates from a linked list
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.
-
Input:
head
of a singly linked list where each node has fields
val
and
next
.
-
Output:
the head of the de-duplicated linked list.
-
Constraints (example):
number of nodes
n
up to
10^5
; node values fit in 32-bit signed int.
Example
-
Input:
1 -> 3 -> 1 -> 2 -> 3 -> null
-
Output:
1 -> 3 -> 2 -> null
Task 2: Longest subsequence of x that is a substring of y
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.
-
Input:
strings
x
,
y
-
Output:
a string
s
meeting the conditions with maximum possible length (or an empty string if no non-empty solution exists).
-
Constraints (example):
1 ≤ |x|, |y| ≤ 2000
; characters are lowercase English letters.
Example
-
x = "abac"
,
y = "zzbaczz"
-
One valid output:
"bac"
(it is a subsequence of
x
and a substring of
y
).