File tree Expand file tree Collapse file tree
src/main/java/leetcode/_83_ Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments