Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 25, 2018 23:34
Show Gist options
  • Select an option

  • Save kanrourou/af8327ff9eb477f73576f7f92a21bc92 to your computer and use it in GitHub Desktop.

Select an option

Save kanrourou/af8327ff9eb477f73576f7f92a21bc92 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* helper = new ListNode(-1);
helper->next = head;
auto fast = head, slow = helper;
while(n--)fast = fast->next;
while(fast)
{
fast = fast->next;
slow = slow->next;
}
auto tmp = slow->next;
slow->next = tmp->next;
tmp->next = nullptr;
return helper->next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment