-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1474.java
More file actions
40 lines (40 loc) · 1.12 KB
/
Copy pathSolution1474.java
File metadata and controls
40 lines (40 loc) · 1.12 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution1474 {
public ListNode deleteNodes(ListNode head, int m, int n) {
ListNode p = head;
int r_num = 0, d_num = 0;
boolean flag = true;
ListNode t = head; // 标记处于删除状态的节点
while (head != null) {
if (flag) {
// 处于保留状态
while (r_num < m && head != null) {
t = head;
head = head.next;
r_num++;
}
flag = false;
r_num = 0;
} else {
// 处于删除状态
while(d_num < n && head != null) {
head = head.next;
d_num++;
}
flag = true;
d_num = 0;
t.next = head;
}
}
return p;
}
}