-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday32.java
More file actions
25 lines (23 loc) · 729 Bytes
/
Copy pathday32.java
File metadata and controls
25 lines (23 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Q1: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
ListNode curr = head;
while (curr != null) {
if (curr.next != null && curr.val == curr.next.val) {
while (curr.next != null && curr.val == curr.next.val) {
curr = curr.next;
}
prev.next = curr.next;
} else {
prev = prev.next;
}
curr = curr.next;
}
return dummy.next;
}
}
TC-O(N)
SC-O(1)