Skip to content

Commit fbe6fe8

Browse files
committed
feat: add 083
1 parent 53e28a2 commit fbe6fe8

File tree

6 files changed

+138
-25
lines changed

6 files changed

+138
-25
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
|67|[Add Binary][067]|Math, String|
2525
|69|[Sqrt(x)][069]|Binary Search, Math|
2626
|70|[Climbing Stairs][070]|Dynamic Programming|
27+
|83|[Remove Duplicates from Sorted List][083]|Linked List|
2728

2829

2930
## Medium
@@ -66,3 +67,4 @@
6667
[067]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/067/README.md
6768
[069]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/069/README.md
6869
[070]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/070/README.md
70+
[083]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/083/README.md

note/070/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Each time you can either climb 1 or 2 steps. In how many distinct ways can you c
88

99
**Note:** Given *n* will be a positive integer.
1010

11-
**Tags:** Binary Search, Math
11+
**Tags:** Dynamic Programming
1212

1313

1414
## 思路

note/083/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [Remove Duplicates from Sorted List][title]
2+
3+
## Description
4+
5+
Given a sorted linked list, delete all duplicates such that each element appear only *once*.
6+
7+
For example,
8+
Given `1->1->2`, return `1->2`.
9+
Given `1->1->2->3->3`, return `1->2->3`.
10+
11+
**Tags:** Linked List
12+
13+
14+
## 思路
15+
16+
题意是删除链表中重复的元素,很简单,我们只需要遍历一遍链表,遇到链表中相邻元素相同时,把当前指针指向下下个元素即可。
17+
18+
``` java
19+
/**
20+
* Definition for singly-linked list.
21+
* public class ListNode {
22+
* int val;
23+
* ListNode next;
24+
* ListNode(int x) { val = x; }
25+
* }
26+
*/
27+
public class Solution {
28+
public ListNode deleteDuplicates(ListNode head) {
29+
if (head == null || head.next == null) return head;
30+
ListNode curr = head;
31+
while (curr.next != null) {
32+
if (curr.next.val == curr.val) {
33+
curr.next = curr.next.next;
34+
} else {
35+
curr = curr.next;
36+
}
37+
}
38+
return head;
39+
}
40+
}
41+
```
42+
43+
44+
## 结语
45+
46+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
47+
48+
49+
50+
[title]: https://leetcode.com/problems/remove-duplicates-from-sorted-list
51+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/LeetCode/leetcode/src/main/java/com/blankj/easy/_021/Solution.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.blankj.easy._021;
22

3+
import com.blankj.structure.ListNode;
4+
35
/**
46
* <pre>
57
* author: Blankj
@@ -10,27 +12,6 @@
1012
*/
1113

1214
public class Solution {
13-
14-
static class ListNode {
15-
int val;
16-
ListNode next;
17-
18-
ListNode(int x) {
19-
val = x;
20-
}
21-
22-
@Override
23-
public String toString() {
24-
String str = "[" + String.valueOf(val);
25-
ListNode p = next;
26-
while (p != null) {
27-
str += ", " + String.valueOf(p.val);
28-
p = p.next;
29-
}
30-
return str + "]";
31-
}
32-
}
33-
3415
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
3516
ListNode head = new ListNode(0);
3617
ListNode temp = head;
@@ -50,6 +31,7 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
5031

5132
public static void main(String[] args) {
5233
Solution solution = new Solution();
34+
;
5335
ListNode listNode00 = new ListNode(1);
5436
ListNode listNode01 = new ListNode(3);
5537
ListNode listNode02 = new ListNode(5);
@@ -70,8 +52,10 @@ public static void main(String[] args) {
7052
listNode12.next = listNode13;
7153
listNode13.next = listNode14;
7254
listNode14.next = null;
73-
System.out.println(listNode00.toString());
74-
System.out.println(listNode10.toString());
75-
System.out.println(solution.mergeTwoLists(listNode00, listNode10).toString());
55+
ListNode listNode0 = ListNode.createTestData(new int[]{1, 3, 5, 7, 9});
56+
ListNode listNode1 = ListNode.createTestData(new int[]{2, 4, 6, 8, 10});
57+
System.out.println(listNode0.toString());
58+
System.out.println(listNode1.toString());
59+
System.out.println(solution.mergeTwoLists(listNode0, listNode1).toString());
7660
}
7761
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.blankj.easy._083;
2+
3+
import com.blankj.structure.ListNode;
4+
5+
/**
6+
* <pre>
7+
* author: Blankj
8+
* blog : http://blankj.com
9+
* time : 2017/05/10
10+
* desc :
11+
* </pre>
12+
*/
13+
14+
public class Solution {
15+
public ListNode deleteDuplicates(ListNode head) {
16+
if (head == null || head.next == null) return head;
17+
ListNode curr = head;
18+
while (curr.next != null) {
19+
if (curr.next.val == curr.val) {
20+
curr.next = curr.next.next;
21+
} else {
22+
curr = curr.next;
23+
}
24+
}
25+
return head;
26+
}
27+
28+
public static void main(String[] args) {
29+
Solution solution = new Solution();
30+
31+
System.out.println(solution.deleteDuplicates(ListNode.createTestData(new int[]{1, 1, 2})));
32+
System.out.println(solution.deleteDuplicates(ListNode.createTestData(new int[]{1, 1, 2, 3, 3})));
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.blankj.structure;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/05/18
8+
* desc :
9+
* </pre>
10+
*/
11+
public class ListNode {
12+
13+
public int val;
14+
public ListNode next;
15+
16+
public ListNode(int x) {
17+
val = x;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
String str = "[" + String.valueOf(val);
23+
ListNode p = next;
24+
while (p != null) {
25+
str += ", " + String.valueOf(p.val);
26+
p = p.next;
27+
}
28+
return str + "]";
29+
}
30+
31+
public static ListNode createTestData(int[] data) {
32+
int len = len = data.length;
33+
if (len == 0) return null;
34+
ListNode[] listNode = new ListNode[len + 1];
35+
listNode[0] = new ListNode(data[0]);
36+
for (int i = 1; i < len; i++) {
37+
listNode[i] = new ListNode(data[i]);
38+
listNode[i - 1].next = listNode[i];
39+
}
40+
return listNode[0];
41+
}
42+
}

0 commit comments

Comments
 (0)