Skip to content

Instantly share code, notes, and snippets.

@bunnyadad
Created April 30, 2019 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunnyadad/e0e6149643b1605b891d6df8ac088e0e to your computer and use it in GitHub Desktop.
Save bunnyadad/e0e6149643b1605b891d6df8ac088e0e 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 dummy(0);
dummy.next = head;
ListNode *fast = &dummy;
ListNode *slow = &dummy;
while (n >= 0)
{
fast = fast->next;
--n;
}
while (fast)
{
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummy.next;
}
};
@bunnyadad
Copy link
Author

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Remove Nth Node From End of List.
Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Remove Nth Node From End of List.
Next challenges:

@bunnyadad
Copy link
Author

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Remove Nth Node From End of List.
Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Remove Nth Node From End of List.
Next challenges:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment