Skip to content

Commit 5480b3a

Browse files
committed
added Remove Duplicates from Sorted List (easy)
1 parent d2b1b0b commit 5480b3a

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Easy.RemoveDuplicatesFromSortedList;
2+
3+
public class Driver {
4+
5+
public static void main(String[] args) {
6+
Solution sol = new Solution();
7+
// test case: 1 -> 1 -> 1 -> 2 -> 2 -> 3 -> 4 -> 5 -> 5
8+
ListNode head = new ListNode(1, new ListNode(1, new ListNode(1, new ListNode(2, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(5)))))))));
9+
System.out.print("Input: ");
10+
printList(head);
11+
12+
head = sol.deleteDuplicates(head);
13+
System.out.print("Output: ");
14+
printList(head);
15+
}
16+
17+
private static void printList(ListNode head) {
18+
while (head != null) {
19+
System.out.print(head.val);
20+
if (head.next != null) {
21+
System.out.print(" -> ");
22+
}
23+
head = head.next;
24+
}
25+
System.out.println();
26+
}
27+
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Remove Duplicates from Sorted List
3+
[Leetcode Link](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
4+
5+
## Problem:
6+
7+
Given a sorted linked list, delete all duplicates such that each element appear *only once*.
8+
9+
## Example:
10+
11+
```
12+
Input: 1->1->2
13+
Output: 1->2
14+
```
15+
```
16+
Input: 1->1->2->3->3
17+
Output: 1->2->3
18+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Easy.RemoveDuplicatesFromSortedList;
2+
3+
// Definition for singly-linked list.
4+
class ListNode {
5+
int val;
6+
ListNode next;
7+
ListNode() {}
8+
ListNode(int val) { this.val = val; }
9+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
10+
}
11+
12+
public class Solution {
13+
14+
public ListNode deleteDuplicates(ListNode head) {
15+
if (head == null || head.next == null) {
16+
return head;
17+
}
18+
ListNode current = head;
19+
ListNode next = head.next;
20+
while (next != null) {
21+
if (current.val == next.val) {
22+
// remove duplicate
23+
current.next = next.next;
24+
next = current.next;
25+
} else {
26+
current = current.next;
27+
next = next.next;
28+
}
29+
}
30+
return head;
31+
}
32+
33+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Languages used: Java and Python
3737
- [Add to Array-Form of Integer](Easy/AddToArrayFormInteger)
3838
- [Sqrt(x)](Easy/SqrtX)
3939
- [Shuffle the Array](Easy/ShuffleArray)
40+
- [Remove Duplicates from Sorted List](Easy/RemoveDuplicatesFromSortedList)
4041
- Medium
4142
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
4243
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)