-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove Nth Node From End of List.cpp
57 lines (54 loc) · 1.26 KB
/
Remove Nth Node From End of List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;
// Definition of ListNode
class ListNode {
public:
int val;
ListNode *next;
ListNode(int val) {
this->val = val;
this->next = NULL;
}
};
class Solution {
public:
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: The head of linked list.
*/
ListNode *removeNthFromEnd(ListNode *head, int n) {
// write your code here
if(!head) return head;
ListNode *dummy = new ListNode(-1);
ListNode *pre = dummy;
dummy->next = head;
ListNode *cur = dummy;
for(int i = 0; i < n; i++) {
cur = cur->next;
if(cur == NULL) return head;
}
while(cur->next) {
pre = pre->next;
cur = cur->next;
}
ListNode *tmp = pre->next;
if(tmp == NULL) pre->next = NULL;
else pre->next = pre->next->next;
delete tmp;
tmp = NULL;
return dummy->next;
}
};
int main() {
ListNode *a = new ListNode(1);
a->next = new ListNode(2);
a->next->next = new ListNode(3);
Solution s;
ListNode *res = s.removeNthFromEnd(a, 5);
while(res) {
cout << res->val << endl;
res = res->next;
}
return 0;
}