Skip to content

Instantly share code, notes, and snippets.

@fjc-oai
Created November 3, 2014 18:45
Show Gist options
  • Save fjc-oai/a88cbd3231ad64ddd43d to your computer and use it in GitHub Desktop.
Save fjc-oai/a88cbd3231ad64ddd43d to your computer and use it in GitHub Desktop.
/**
* 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