-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75db6b9
commit fb555eb
Showing
2 changed files
with
35 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "common.h" | ||
|
||
namespace s0019 { | ||
// Definition for singly-linked list. | ||
struct ListNode { | ||
int val; | ||
ListNode *next; | ||
ListNode() : val(0), next(nullptr) {} | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
}; | ||
class Solution { | ||
public: | ||
ListNode* removeNthFromEnd(ListNode* head, int n) { | ||
ListNode *dummyHead = new ListNode(0, head); | ||
ListNode *slow = dummyHead, *fast = dummyHead; | ||
while (n--) fast = fast->next; | ||
fast = fast->next; | ||
while (fast) { | ||
fast = fast->next; | ||
slow = slow->next; | ||
} | ||
slow->next = slow->next->next; | ||
|
||
return dummyHead->next; | ||
} | ||
}; | ||
} |
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