File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change 45
45
78. 子集
46
46
79. 单词搜索
47
47
82. 删除排序链表中的重复元素 II
48
+ 83. 删除排序链表中的重复元素
48
49
84. 柱状图中最大的矩形
49
50
85. 最大矩形
50
51
88. 合并两个有序数组
Original file line number Diff line number Diff line change 113
113
23. 合并K个升序链表(顺序合并,分治合并)
114
114
25. K 个一组翻转链表(多指针)
115
115
82. 删除排序链表中的重复元素 II(递归,双指针)
116
+ 83. 删除排序链表中的重复元素
116
117
92. 反转链表 II(多指针)
117
118
141. 环形链表(快慢指针,列表,哈希表)
118
119
142. 环形链表 II(快慢指针,列表,哈希表)
Original file line number Diff line number Diff line change
1
+ // 83. 删除排序链表中的重复元素
2
+
3
+
4
+ /**
5
+ * Definition for singly-linked list.
6
+ * public class ListNode {
7
+ * int val;
8
+ * ListNode next;
9
+ * ListNode() {}
10
+ * ListNode(int val) { this.val = val; }
11
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
12
+ * }
13
+ */
14
+
15
+
16
+ /*
17
+ 1、创建哨兵节点标记链表头部,用于删除节点后返回链表
18
+ 2、使用前后两个指针pre、head来标记节点位置,通过判断head不为空来循环遍历,从而保证能获取前后两个节点的值来判断
19
+ */
20
+ class Solution {
21
+ public ListNode deleteDuplicates (ListNode head ) {
22
+ ListNode root = new ListNode (-101 , head );
23
+ ListNode pre = root ;
24
+ while (head != null ) {
25
+ if (head .val == pre .val ) {
26
+ pre .next = head .next ;
27
+ } else {
28
+ pre = head ;
29
+ }
30
+ head = head .next ;
31
+ }
32
+ return root .next ;
33
+ }
34
+ }
35
+
36
+
37
+ /*
38
+ 1、head为链表头部不变,用于删除节点后返回链表
39
+ 2、使用一个指针cur标记节点,通过判断cur和cur.next不为空来循环遍历,从而保证能获取前后两个节点的值来判断
40
+ */
41
+ class Solution {
42
+ public ListNode deleteDuplicates (ListNode head ) {
43
+ ListNode cur = head ;
44
+ while (cur != null && cur .next != null ) {
45
+ if (cur .val == cur .next .val ) {
46
+ cur .next = cur .next .next ;
47
+ } else {
48
+ cur = cur .next ;
49
+ }
50
+ }
51
+ return head ;
52
+ }
53
+ }
You can’t perform that action at this time.
0 commit comments