Skip to content

Commit 700fdf1

Browse files
committed
Add remove-duplicates-from-sorted-list and remove-duplicates-from-sorted-list-ii
1 parent d7be0ae commit 700fdf1

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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/)

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ My collection of leet code problems solved in GoLang!
55

66
| [Easy](#easy) | [Medium](#medium) | [Hard](#hard) |
77
|---------------|-------------------|---------------|
8-
| 35 | 60 | 10 |
8+
| 36 | 62 | 10 |
99

1010
Problems finished
1111
=================
@@ -83,6 +83,8 @@ Easy
8383

8484
- [squares-of-a-sorted-array](Medium/squares-of-a-sorted-array.md)
8585

86+
- [remove-duplicates-from-sorted-list](Medium/remove-duplicates-from-sorted-list.md)
87+
8688
Medium
8789
------
8890

@@ -208,6 +210,8 @@ Medium
208210

209211
- [game-of-life](Medium/game-of-life.md)
210212

213+
- [remove-duplicates-from-sorted-list-ii](Medium/remove-duplicates-from-sorted-list-ii.md)
214+
211215
Hard
212216
----
213217

0 commit comments

Comments
 (0)