Skip to content

Commit

Permalink
Create 1474.Delete-N-Nodes-After-M-Nodes-of-a-Linked-List.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Jul 14, 2020
1 parent a29109d commit 2323d4e
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteNodes(ListNode* head, int m, int n)
{
ListNode* p = head;
while (p)
{
for (int i=0; i<m-1; i++)
{
p = p->next;
if (!p) break;
}

if (!p) break;

ListNode* q = p;
for (int i=0; i<n; i++)
{
q = q->next;
if (!q) break;
}
if (!q)
{
p->next = NULL;
break;
}
else
{
p->next = q->next;
p = p->next;
}
}

return head;

}
};

0 comments on commit 2323d4e

Please sign in to comment.