-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday32.java
More file actions
85 lines (57 loc) · 1.69 KB
/
day32.java
File metadata and controls
85 lines (57 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//ques1:82. Remove Duplicates from Sorted List II
//link:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
ListNode cur = head;
while (cur != null) {
while (cur.next != null && cur.val == cur.next.val) {
cur = cur.next;
}
if (prev.next == cur) {
prev = prev.next;
} else {
prev.next = cur.next;
}
cur = cur.next;
}
return dummy.next;
}
}
//ques2:25. Reverse Nodes in k-Group
//link:https://leetcode.com/problems/reverse-nodes-in-k-group/description/
class Solution2 {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode cur = head;
ListNode prev = null;
ListNode temp, temp2, ans = head, pl = null;
int count = 0, n = 1, i = 1;
while (cur != null) {
count++;
cur = cur.next;
}
cur = head;
while (n * k <= count) {
temp = cur;
i = 1;
prev = null;
while (i <= k) {
temp2 = cur.next;
cur.next = prev;
prev = cur;
cur = temp2;
i++;
}
temp.next = cur;
if (n >= 2)
pl.next = prev;
if (n == 1)
ans = prev;
pl = temp;
n++;
}
return ans;
}
}