-
-
Save emad-elsaid/d92c053f2045902c5637 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; | |
* 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