Skip to content

Commit b917e34

Browse files
committed
0083. Remove Duplicates from Sorted List
1 parent fb3dd60 commit b917e34

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [83\. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a sorted linked list, delete all duplicates such that each element appear only _once_.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: 1->1->2
12+
Output: 1->2
13+
```
14+
15+
**Example 2:**
16+
17+
```
18+
Input: 1->1->2->3->3
19+
Output: 1->2->3
20+
```
21+
22+
23+
#### Solution
24+
25+
Language: **Java**
26+
27+
```java
28+
/**
29+
* Definition for singly-linked list.
30+
* public class ListNode {
31+
* int val;
32+
* ListNode next;
33+
* ListNode(int x) { val = x; }
34+
* }
35+
*/
36+
class Solution {
37+
   public ListNode deleteDuplicates(ListNode head) {
38+
       if (head == null) {
39+
           return null;
40+
      }
41+
       ListNode curr = head;
42+
       ListNode nextIndex = head;
43+
       while (curr.next != null) {
44+
           while (curr.next != null && curr.val == curr.next.val) {
45+
               curr = curr.next;
46+
          }
47+
           if (curr != nextIndex) {
48+
               nextIndex.next = curr.next;
49+
               nextIndex = curr;
50+
          } else {
51+
               curr = curr.next;
52+
               nextIndex = nextIndex.next;
53+
          }
54+
      }
55+
       return head;
56+
  }
57+
}
58+
```
59+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-30-5KXuLX.jpg)

src/main/java/leetcode/_83_/Main.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode._83_;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
12+
ListNode node1 = new ListNode(1);
13+
ListNode node2 = new ListNode(1);
14+
ListNode node31 = new ListNode(2);
15+
ListNode node32 = new ListNode(3);
16+
ListNode node41 = new ListNode(4);
17+
ListNode node42 = new ListNode(4);
18+
ListNode node5 = new ListNode(5);
19+
node1.next = node2;
20+
node2.next = node31;
21+
node31.next = node32;
22+
node32.next = node41;
23+
node41.next = node42;
24+
node42.next = node5;
25+
26+
27+
System.out.println(solution.deleteDuplicates(node1));
28+
}
29+
}
30+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode._83_;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) { val = x; }
11+
* }
12+
*/
13+
class Solution {
14+
public ListNode deleteDuplicates(ListNode head) {
15+
if (head == null) {
16+
return null;
17+
}
18+
ListNode curr = head;
19+
while (curr.next != null) {
20+
if (curr.val == curr.next.val) {
21+
22+
}
23+
}
24+
return head;
25+
}
26+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
### [83\. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a sorted linked list, delete all duplicates such that each element appear only _once_.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: 1->1->2
12+
Output: 1->2
13+
```
14+
15+
**Example 2:**
16+
17+
```
18+
Input: 1->1->2->3->3
19+
Output: 1->2->3
20+
```
21+
22+
23+
#### Solution
24+
25+
Language: **Java**
26+
27+
```java
28+
/**
29+
* Definition for singly-linked list.
30+
* public class ListNode {
31+
* int val;
32+
* ListNode next;
33+
* ListNode(int x) { val = x; }
34+
* }
35+
*/
36+
class Solution {
37+
   public ListNode deleteDuplicates(ListNode head) {
38+
       if (head == null) {
39+
           return null;
40+
      }
41+
       ListNode curr = head;
42+
       ListNode nextIndex = head;
43+
       while (curr.next != null) {
44+
           while (curr.next != null && curr.val == curr.next.val) {
45+
               curr = curr.next;
46+
          }
47+
           if (curr != nextIndex) {
48+
               nextIndex.next = curr.next;
49+
               nextIndex = curr;
50+
          } else {
51+
               curr = curr.next;
52+
               nextIndex = nextIndex.next;
53+
          }
54+
      }
55+
       return head;
56+
  }
57+
}
58+
```
59+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-30-5KXuLX.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy