-
Notifications
You must be signed in to change notification settings - Fork 0
/
1474. Delete N Nodes After M Nodes of a Linked List.cpp
86 lines (77 loc) · 2.58 KB
/
1474. Delete N Nodes After M Nodes of a Linked List.cpp
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
84
85
86
// ***
//
// Given the head of a linked list and two integers m and n. Traverse the linked list and remove some nodes in the
// following way:
//
// Start with the head as the current node.
// Keep the first m nodes starting with the current node.
// Remove the next n nodes
// Keep repeating steps 2 and 3 until you reach the end of the list.
// Return the head of the modified list after removing the mentioned nodes.
//
// Example 1:
//
// Given the head of a linked list and two integers m and n. Traverse the linked list and remove some nodes in the
// following way:
//
// Start with the head as the current node.
// Keep the first m nodes starting with the current node.
// Remove the next n nodes
// Keep repeating steps 2 and 3 until you reach the end of the list.
// Return the head of the modified list after removing the mentioned nodes.
//
// Example 2:
//
// Input: head = [1,2,3,4,5,6,7,8,9,10,11], m = 1, n = 3
// Output: [1,5,9]
// Explanation: Head of linked list after removing nodes is returned.
//
// Example 3:
//
// Input: head = [1,2,3,4,5,6,7,8,9,10,11], m = 3, n = 1
// Output: [1,2,3,5,6,7,9,10,11]
//
// Example 4:
//
// Input: head = [9,3,7,7,9,10,8,2], m = 1, n = 2
// Output: [9,7,8]
//
// Constraints:
//
// The given linked list will contain between 1 and 10^4 nodes.
// The value of each node in the linked list will be in the range [1, 10^6].
// 1 <= m,n <= 1000
//
// ***
// See also 19. Remove Nth Node from End of List.
class Solution {
public:
ListNode* deleteNodes(ListNode* head, int m, int n) {
ListNode *slow = head, *fast = head;
while (fast) {
int mTemp = m, nTemp = n;
// fast is now the 1 st node
// slow is now the 1 st node
while (fast and mTemp--) {
// We update slow 1 step "slower" here because
// we want slow to be the m th node (so it points to the m + 1 th node).
slow = fast;
fast = fast->next;
}
// fast is now the m + 1 th node
// slow is now the m th node
while (fast and nTemp--) {
ListNode* toBeDeleted = fast;
fast = fast->next;
delete toBeDeleted;
}
// fast is now the m + n + 1 th node
// slow is now the m th node
// Remove all elements between slow and fast.
// the m th node now points to the m + n + 1 th elements.
// So we kept the first m elements and removed the n elements in between.
slow->next = fast;
}
return head;
}
};