Created
November 3, 2014 18:45
-
-
Save fjc-oai/a88cbd3231ad64ddd43d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode *removeNthFromEnd(ListNode *head, int n) { | |
ListNode*h=new ListNode(0),*p=h,*q=h; | |
h->next=head; | |
for(int i=0;i<n+1;i++) | |
p=p->next; | |
while(p!=NULL){ | |
p=p->next; | |
q=q->next; | |
} | |
q->next=q->next->next; | |
p=h->next; | |
delete(h); | |
return p; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment