-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_LC19.cpp
80 lines (76 loc) · 1.75 KB
/
list_LC19.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
#include<iostream>
using namespace std;
#include<list>
struct ListNode {
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};
class Solution {
public:
ListNode * removeNthFromEnd(ListNode* head, int n) {
ListNode* succ=head;
int totalNum = 1;
while (succ->next != NULL) {
succ = succ->next;
totalNum++;
}
//cout << totalNum;
//ListNode* purpose;
//ListNode* succ2 = head;
int fromBegin = totalNum - n + 1;
//cout << fromBegin;
ListNode* pred = head;
//only need next fromBegin-1 times
//remove first num
if (fromBegin==1) {
/*
head = head->next;
return head;
*/
return head->next;
}
//else
while (fromBegin-- > 2) {//first find the purpose's pred
pred = pred->next;
}
//purpose = pred->next;
//cout << purpose->val;
//succ2 = purpose->next;
pred->next = pred->next->next;
return head;
}
};
class Solution {
public:
ListNode * removeNthFromEnd(ListNode* head, int n) {
//move n times
ListNode* first = head;
for (int i = 0; i < n; i++) {
first = first->next;
}
if (first == NULL) {
//it shows that remove the first node
return head->next;
}
ListNode* second = head;
//then the second only need to move total-n to get the node
//and first move total-n to get end(NULL)
//second need to move total-n-1 times to get the pred node
while (first->next != NULL) {//totally totalNum-1 times
first = first->next;
second = second->next;
}
//now the second is the pred of the purpose node
second->next = second->next->next;
return head;
}
};