Remove Duplicates from a Sorted Doubly Linked List
Solve this ProblemTest Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ number of nodes in head ≤ 200 - ◆
-1000 ≤ node value ≤ 1000 - ◆
head is already sorted in non-decreasing order, so every duplicate of a value sits consecutively
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
Approach & Solutions
Brute Force — Filter Consecutive Duplicates into a New Array
GoodWalk the list once, copying a value into an output array only when it differs from the array's last entry so far (skipping it whenever it repeats the previous value), then build a brand-new list from that array. Straightforward, but it allocates an array (and a whole new chain of nodes) the size of the deduplicated list.
O(n)O(n)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node removeDupSortedDLL(Node head) {
9 List<Integer> kept = new ArrayList<>();
10 Node curr = head;
11 while (curr != null) {
12 if (kept.isEmpty() || kept.get(kept.size() - 1) != curr.val) {
13 kept.add(curr.val);
14 }
15 curr = curr.next;
16 }
17 Node dummy = new Node(0);
18 Node tail = dummy;
19 for (int v : kept) {
20 tail.next = new Node(v);
21 tail = tail.next;
22 }
23 return dummy.next;
24 }
25}Optimal — Bypass a Repeat as Soon as It's Seen
OptimalBecause the list is already sorted, every duplicate of a value is right next to it — there's no need to remember anything beyond the current node. Compare each node to the one after it: while the next node holds the same value, skip straight past it by redirecting curr.next; only advance curr once the next node's value actually differs. In a true doubly linked list, the node that survives would also get its .prev kept pointed correctly.
O(n)O(1) extra1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node removeDupSortedDLL(Node head) {
9 Node curr = head;
10 while (curr != null && curr.next != null) {
11 if (curr.val == curr.next.val) {
12 curr.next = curr.next.next;
13 } else {
14 curr = curr.next;
15 }
16 }
17 return head;
18 }
19}