Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save toliuweijing/6151758 to your computer and use it in GitHub Desktop.
Save toliuweijing/6151758 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 eh(0);
eh.next = head;
auto handle = &eh;
for (auto p = head; p; p=p->next) {
if (n-- <= 0) { handle = handle->next; }
}
handle->next = handle->next->next;
return eh.next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment