File tree Expand file tree Collapse file tree 3 files changed +82
-1
lines changed Expand file tree Collapse file tree 3 files changed +82
-1
lines changed Original file line number Diff line number Diff line change
1
+ Code
2
+ ====
3
+
4
+ ``` go
5
+ /* *
6
+ * Definition for singly-linked list.
7
+ * type ListNode struct {
8
+ * Val int
9
+ * Next *ListNode
10
+ * }
11
+ */
12
+ func deleteDuplicates (head *ListNode ) *ListNode {
13
+ curr := head
14
+ temp := curr
15
+
16
+ for temp != nil {
17
+ for temp != nil && temp.Val == curr.Val {
18
+ temp = temp.Next
19
+ }
20
+ curr.Next = temp
21
+ curr = temp
22
+ }
23
+
24
+ return head
25
+ }
26
+ ```
27
+
28
+ Solution in mind
29
+ ================
30
+
31
+ - Make use of a temp pointer to skip duplicate nodes.
32
+
33
+ - After skipping of nodes, link the previous pointer to it's new node.
Original file line number Diff line number Diff line change
1
+ Code
2
+ ====
3
+
4
+ ``` go
5
+ /* *
6
+ * Definition for singly-linked list.
7
+ * type ListNode struct {
8
+ * Val int
9
+ * Next *ListNode
10
+ * }
11
+ */
12
+ func deleteDuplicates (head *ListNode ) *ListNode {
13
+
14
+ if head == nil || head.Next == nil {
15
+ return head
16
+ }
17
+
18
+ sentinal := &ListNode{-1 , head}
19
+
20
+ prev := sentinal
21
+ curr := head
22
+
23
+ for curr != nil {
24
+
25
+ if curr.Next != nil && curr.Val == curr.Next .Val {
26
+ for curr.Next != nil && curr.Val == curr.Next .Val {
27
+ curr = curr.Next
28
+ }
29
+ prev.Next = curr.Next
30
+ } else {
31
+ prev = prev.Next
32
+ }
33
+
34
+ curr = curr.Next
35
+ }
36
+
37
+ return sentinal.Next
38
+ }
39
+ ```
40
+
41
+ Solution in mind
42
+ ================
43
+
44
+ - Solution as described [ here] ( https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/solution/ )
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ My collection of leet code problems solved in GoLang!
5
5
6
6
| [ Easy] ( #easy ) | [ Medium] ( #medium ) | [ Hard] ( #hard ) |
7
7
| ---------------| -------------------| ---------------|
8
- | 35 | 60 | 10 |
8
+ | 36 | 62 | 10 |
9
9
10
10
Problems finished
11
11
=================
83
83
84
84
- [ squares-of-a-sorted-array] ( Medium/squares-of-a-sorted-array.md )
85
85
86
+ - [ remove-duplicates-from-sorted-list] ( Medium/remove-duplicates-from-sorted-list.md )
87
+
86
88
Medium
87
89
------
88
90
@@ -208,6 +210,8 @@ Medium
208
210
209
211
- [ game-of-life] ( Medium/game-of-life.md )
210
212
213
+ - [ remove-duplicates-from-sorted-list-ii] ( Medium/remove-duplicates-from-sorted-list-ii.md )
214
+
211
215
Hard
212
216
----
213
217
You can’t perform that action at this time.
0 commit comments