Skip to content

Commit fb3dd60

Browse files
committed
0082. Remove Duplicates from Sorted List II
1 parent 75f89c6 commit fb3dd60

File tree

3 files changed

+94
-76
lines changed

3 files changed

+94
-76
lines changed

src/main/java/leetcode/_81_/Main.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
package leetcode._81_;
22

3+
import leetcode.common.ListNode;
4+
35
/**
46
* Created by zhangbo54 on 2019-03-04.
57
*/
68
public class Main {
79
public static void main(String[] args) {
10+
811
Solution solution = new Solution();
9-
int[] nums = {2, 5, 6, 0, 0, 1, 2};
10-
System.out.println(solution.search(nums, 2));
11-
System.out.println(solution.search(nums, 5));
12-
System.out.println(solution.search(nums, 6));
13-
System.out.println(solution.search(nums, 0));
14-
System.out.println(solution.search(nums, 1));
15-
System.out.println(solution.search(nums, 2));
16-
int[] nums2 = {2,5,6,0,0,1,2};
17-
System.out.println(solution.search(nums2, 3));
12+
13+
// ListNode node1 = new ListNode(1);
14+
// ListNode node2 = new ListNode(2);
15+
// ListNode node31 = new ListNode(3);
16+
// ListNode node32 = new ListNode(3);
17+
// ListNode node41 = new ListNode(4);
18+
// ListNode node42 = new ListNode(4);
19+
// ListNode node5 = new ListNode(5);
20+
// node1.next = node2;
21+
// node2.next = node31;
22+
// node31.next = node32;
23+
// node32.next = node41;
24+
// node41.next = node42;
25+
// node42.next = node5;
26+
27+
//
28+
// System.out.println(solution.deleteDuplicates(node1));
29+
//
30+
// ListNode node11 = new ListNode(1);
31+
// ListNode node12 = new ListNode(1);
32+
// node11.next = node12;
33+
// System.out.println(solution.deleteDuplicates(node11));
34+
35+
36+
ListNode node1 = new ListNode(1);
37+
ListNode node21 = new ListNode(1);
38+
ListNode node22 = new ListNode(2);
39+
node1.next = node21;
40+
node21.next = node22;
41+
System.out.println(solution.deleteDuplicates(node1));
1842
}
1943
}
2044

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
package leetcode._81_;
22

3-
import java.util.Arrays;
3+
import leetcode.common.ListNode;
44

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+
*/
513
class Solution {
6-
public boolean search(int[] nums, int target) {
7-
int low = 0;
8-
int high = nums.length - 1;
9-
int middle;
10-
while (low <= high) {
11-
middle = (high + low) / 2;
12-
if (nums[middle] == target) {
13-
return true;
14+
public ListNode deleteDuplicates(ListNode head) {
15+
if (head == null) {
16+
return null;
17+
}
18+
ListNode fakeHead = new ListNode(0);
19+
fakeHead.next = head;
20+
ListNode pre = fakeHead;
21+
ListNode curr = head;
22+
while (curr != null) {
23+
while (curr.next != null && curr.val == curr.next.val) {
24+
curr = curr.next;
1425
}
15-
if (nums[low] < nums[middle] || nums[high] < nums[middle]) { // [low,middle]是升序 或者是[middle,high]
16-
if (target >= nums[low] && nums[middle] >= target) { // 普通二分查找就能得到结果
17-
high = middle - 1;
18-
} else {
19-
low = middle + 1;
20-
}
21-
22-
} else if (nums[high] > nums[middle] || nums[low] > nums[middle]) { // [low,middle] 必然有对称点
23-
if (target <= nums[high] && nums[middle] <= target) { // 普通二分查找就能得到结果
24-
low = middle + 1;
25-
} else {
26-
high = middle - 1;
27-
}
28-
29-
} else { // nums[low] == nums[middle] 无法判断是升序还是有对称点
30-
high--;
26+
if (pre.next == curr) { // 说明该次循环没有重复元素
27+
pre = pre.next;
28+
} else {
29+
pre.next = curr.next;
3130
}
31+
curr = curr.next;
32+
3233
}
33-
return false;
34+
return fakeHead.next;
3435
}
3536
}
Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,61 @@
1-
### [81\. Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)
1+
### [82\. Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)
22

33
Difficulty: **Medium**
44

55

6-
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
7-
8-
(i.e., `[0,0,1,2,2,5,6]` might become `[2,5,6,0,0,1,2]`).
9-
10-
You are given a target value to search. If found in the array return `true`, otherwise return `false`.
6+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only _distinct_ numbers from the original list.
117

128
**Example 1:**
139

1410
```
15-
Input: nums = [2,5,6,0,0,1,2], target = 0
16-
Output: true
11+
Input: 1->2->3->3->4->4->5
12+
Output: 1->2->5
1713
```
1814

1915
**Example 2:**
2016

2117
```
22-
Input: nums = [2,5,6,0,0,1,2], target = 3
23-
Output: false```
24-
25-
**Follow up:**
26-
27-
* This is a follow up problem to , where `nums` may contain duplicates.
28-
* Would this affect the run-time complexity? How and why?
18+
Input: 1->1->1->2->3
19+
Output: 2->3
20+
```
2921

3022

3123
#### Solution
3224

3325
Language: **Java**
3426

3527
```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+
*/
3636
class Solution {
37-
   public boolean search(int[] nums, int target) {
38-
       int low = 0;
39-
       int high = nums.length - 1;
40-
       int middle;
41-
       while (low <= high) {
42-
           middle = (high + low) / 2;
43-
           if (nums[middle] == target) {
44-
               return true;
37+
   public ListNode deleteDuplicates(ListNode head) {
38+
       if (head == null) {
39+
           return null;
40+
      }
41+
       ListNode fakeHead = new ListNode(0);
42+
       fakeHead.next = head;
43+
       ListNode pre = fakeHead;
44+
       ListNode curr = head;
45+
       while (curr != null) {
46+
           while (curr.next != null && curr.val == curr.next.val) {
47+
               curr = curr.next;
4548
          }
46-
           if (nums[low] < nums[middle] || nums[high] < nums[middle]) { // [low,middle]是升序 或者是[middle,high]
47-
               if (target >= nums[low] && nums[middle] >= target) { // 普通二分查找就能得到结果
48-
                   high = middle - 1;
49-
              } else {
50-
                   low = middle + 1;
51-
              }
52-
53-
          } else if (nums[high] > nums[middle] || nums[low] > nums[middle]) { // [low,middle] 必然有对称点
54-
               if (target <= nums[high] && nums[middle] <= target) { // 普通二分查找就能得到结果
55-
                   low = middle + 1;
56-
              } else {
57-
                   high = middle - 1;
58-
              }
59-
60-
          } else { // nums[low] == nums[middle] 无法判断是升序还是有对称点
61-
               high--;
49+
           if (pre.next == curr) { // 说明该次循环没有重复元素
50+
               pre = pre.next;
51+
          } else {
52+
               pre.next = curr.next;
6253
          }
54+
           curr = curr.next;
55+
6356
      }
64-
       return false;
57+
       return fakeHead.next;
6558
  }
6659
}
6760
```
68-
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-29-fwUbnO.jpg)
61+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-29-bjF2FD.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