Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Created May 17, 2015 13:26
Show Gist options
  • Save emad-elsaid/d92c053f2045902c5637 to your computer and use it in GitHub Desktop.
Save emad-elsaid/d92c053f2045902c5637 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int remov(struct ListNode* current, int n);
int remov(struct ListNode* current, int n){
if(current==0){
return 0;
}
int count = remov(current->next, n)+1;
if(count == (n+1) ){
current->next = current->next->next;
}
return count;
}
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
int length = remov(head, n);
return (n==length)? head->next : head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment