Skip to content

Commit b3c481a

Browse files
authored
1474. Delete N Nodes After M Nodes of a Linked List
1 parent 782c0b8 commit b3c481a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Linked List/deleteNodes.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
ListNode* deleteNodes(ListNode* head, int m, int n) {
4+
if(!head || !head->next) return head;
5+
ListNode* temp = head;
6+
while(temp && temp->next) {
7+
int k = m-1;
8+
while(k-- && temp) {
9+
temp = temp->next;
10+
}
11+
ListNode* prev = temp;
12+
int l = n+1;
13+
while(l-- && temp) {
14+
temp = temp->next;
15+
}
16+
if(prev) prev->next = temp;
17+
}
18+
19+
return head;
20+
}
21+
};

0 commit comments

Comments
 (0)