Created
April 30, 2019 02:29
-
-
Save bunnyadad/e0e6149643b1605b891d6df8ac088e0e to your computer and use it in GitHub Desktop.
This file contains 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; | |
* 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; | |
} | |
}; |
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
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: