Skip to content

Commit fcfad00

Browse files
author
Yi Gu
committed
[LinkedList] Optimize solutions
1 parent cb0374a commit fcfad00

3 files changed

+19
-36
lines changed

LinkedList/RemoveDuplicatesFromSortedList.swift

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Question Link: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
3-
* Primary idea: Two pointers, iterate the list and only reserve right nodes
3+
* Primary idea: Iterate the list, jump over duplicates by replacing next with next.next
44
* Time Complexity: O(n), Space Complexity: O(1)
55
*
66
* Definition for singly-linked list.
@@ -15,22 +15,20 @@
1515
*/
1616

1717
class RemoveDuplicatesFromSortedList {
18-
func deleteDuplicates(head: ListNode?) -> ListNode? {
18+
func deleteDuplicates(_ head: ListNode?) -> ListNode? {
1919
guard let head = head else {
2020
return nil
2121
}
2222

23-
var prev = head
24-
var curr = head.next
23+
var curt = head
2524

26-
while curr != nil {
27-
if prev.val != curr!.val {
28-
prev.next = curr
29-
prev = curr!
30-
}
31-
curr = curr!.next
25+
while curt.next != nil {
26+
if curt.next!.val == curt.val {
27+
curt.next = curt.next!.next
28+
} else {
29+
curt = curt.next!
30+
}
3231
}
33-
prev.next = nil
3432

3533
return head
3634
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* Question Link: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
3-
* Primary idea: Three pointers, iterate the list and only reserve right nodes,
4-
* specially handle the last one afterwards
3+
* Primary idea: Iterate the list, jump over duplicates by replacing next with next.next
54
*
65
* Note: Swift provides "===" to compare two objects refer to the same reference
76
*
@@ -19,40 +18,26 @@
1918
*/
2019

2120
class RemoveDuplicatesfromSortedListII {
22-
func deleteDuplicates(head: ListNode?) -> ListNode? {
21+
func deleteDuplicates(_ head: ListNode?) -> ListNode? {
2322
if head == nil || head!.next == nil {
2423
return head
2524
}
2625

2726
let dummy = ListNode(0)
2827
dummy.next = head
29-
3028
var node = dummy
31-
var prev = head
32-
var post = head!.next
3329

34-
while post != nil {
35-
if post!.val != prev!.val && prev!.next === post{
36-
node.next = prev
37-
node = prev!
38-
prev = post!
39-
post = post!.next
40-
} else {
41-
if post!.val != prev!.val {
42-
prev = post!
43-
post = post!.next
44-
} else {
45-
post = post!.next
30+
while node.next != nil && node.next!.next != nil {
31+
if node.next!.val == node.next!.next!.val {
32+
let val = node.next!.val
33+
while node.next != nil && node.next!.val == val {
34+
node.next = node.next!.next
4635
}
36+
} else {
37+
node = node.next!
4738
}
4839
}
4940

50-
if prev!.next != nil {
51-
node.next = nil
52-
} else {
53-
node.next = prev
54-
}
55-
5641
return dummy.next
5742
}
5843
}

LinkedList/RemoveLinkedListElements.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Question Link: https://leetcode.com/problems/remove-linked-list-elements/
3-
* Primary idea: Two pointers, iterate the list and only reserve right nodes
3+
* Primary idea: Iterate the list, jump over vals by replacing next with next.next
44
* Time Complexity: O(n), Space Complexity: O(1)
55
*
66
* Definition for singly-linked list.

0 commit comments

Comments
 (0)