-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-nth-node-from-end-of-list.cpp
57 lines (51 loc) · 1.27 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//使用newhead的目的是为了处理删除头结点的情况:
// [1,2] 2
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* newhead=new ListNode(-100);
newhead->next=head;
ListNode *s=newhead;
ListNode *f=head;
int t=1;
while(t<=n){
f=f->next;
t++;
}//使得快慢指针之间相差n+1步。
while(f){
f=f->next;
s=s->next;
}
s->next=s->next->next;
return newhead->next;
}
//很暴力求解,直接画图做的,根本没有什么思考
ListNode* removeNthFromEnd1(ListNode* head, int n) {
ListNode* cur=head;
int len=1;
while(cur){
len++;
cur=cur->next;
}
len--;
if(len==n) return head->next;
int tmp=1;
cur=head;
len-=n;
while(tmp<len){
tmp++;
cur=cur->next;
}
if(cur->next) cur->next=cur->next->next;
else cur->next=nullptr;
return head;
}
};