Skip to content

Commit 8e83ae9

Browse files
EASY/src/easy/RemoveDuplicatesfromSortedList.java
1 parent f0e41cb commit 8e83ae9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package easy;
2+
3+
import utils.CommonUtils;
4+
import classes.ListNode;
5+
6+
public class RemoveDuplicatesfromSortedList {
7+
8+
public static ListNode deleteDuplicates(ListNode head) {
9+
ListNode ret = new ListNode(-1);
10+
ret.next = head;
11+
while(head != null){
12+
while(head.next != null && head.next.val == head.val){
13+
head.next = head.next.next;
14+
}
15+
head = head.next;
16+
}
17+
return ret.next;
18+
}
19+
20+
public static void main(String...strings){
21+
ListNode head = new ListNode(1);
22+
head.next = new ListNode(1);
23+
head.next.next = new ListNode(2);
24+
head.next.next.next = new ListNode(3);
25+
head.next.next.next.next = new ListNode(3);
26+
CommonUtils.printList(head);
27+
ListNode result = deleteDuplicates(head);
28+
CommonUtils.printList(result);
29+
}
30+
}

0 commit comments

Comments
 (0)