Skip to content

Commit e68f8b4

Browse files
committed
solve 83.remove-duplicates-from-sorted-list
1 parent b03657f commit e68f8b4

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @lc app=leetcode id=83 lang=java
3+
*
4+
* [83] Remove Duplicates from Sorted List
5+
*
6+
* https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
7+
*
8+
* algorithms
9+
* Easy (43.24%)
10+
* Likes: 915
11+
* Dislikes: 86
12+
* Total Accepted: 363.5K
13+
* Total Submissions: 838K
14+
* Testcase Example: '[1,1,2]'
15+
*
16+
* Given a sorted linked list, delete all duplicates such that each element
17+
* appear only once.
18+
*
19+
* Example 1:
20+
*
21+
*
22+
* Input: 1->1->2
23+
* Output: 1->2
24+
*
25+
*
26+
* Example 2:
27+
*
28+
*
29+
* Input: 1->1->2->3->3
30+
* Output: 1->2->3
31+
*
32+
*
33+
*/
34+
/**
35+
* Definition for singly-linked list.
36+
* public class ListNode {
37+
* int val;
38+
* ListNode next;
39+
* ListNode(int x) { val = x; }
40+
* }
41+
*/
42+
class Solution {
43+
public ListNode deleteDuplicates(ListNode head) {
44+
if (head == null) {
45+
return null;
46+
}
47+
ListNode prev = head;
48+
ListNode curr = head.next;
49+
while (prev != null && curr != null) {
50+
if (prev.val == curr.val) {
51+
prev.next = curr.next;
52+
} else {
53+
prev = curr;
54+
}
55+
curr = curr.next;
56+
}
57+
return head;
58+
}
59+
}
60+

0 commit comments

Comments
 (0)