-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.cpp
36 lines (33 loc) · 813 Bytes
/
19.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
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(head==NULL)return head;
ListNode *node=new ListNode(0,head);
node->next=head;
ListNode *fast=new ListNode(0,head);
ListNode *slow=new ListNode(0,head);
int c=0;
while(fast->next->next!=NULL)
{
c++;
if(c<=n)
{
fast->next=fast->next->next;
}
else
{
fast->next=fast->next->next;
slow->next=slow->next->next;
}
}
if(n-c==1)
{
return head->next;
}
else
{
slow->next->next=slow->next->next->next;
return head;
}
}
};