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 454578. 子集
464679. 单词搜索
474782. 删除排序链表中的重复元素 II
48+ 83. 删除排序链表中的重复元素
484984. 柱状图中最大的矩形
495085. 最大矩形
505188. 合并两个有序数组
Original file line number Diff line number Diff line change 11311323. 合并K个升序链表(顺序合并,分治合并)
11411425. K 个一组翻转链表(多指针)
11511582. 删除排序链表中的重复元素 II(递归,双指针)
116+ 83. 删除排序链表中的重复元素
11611792. 反转链表 II(多指针)
117118141. 环形链表(快慢指针,列表,哈希表)
118119142. 环形链表 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