-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-19.cpp
More file actions
43 lines (41 loc) · 997 Bytes
/
LeetCode-19.cpp
File metadata and controls
43 lines (41 loc) · 997 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
//code 1
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
struct ListNode *p = head, *q = head;
for(int i = 0; i < n; i++){
p = p->next;
}
if(p == NULL){
head = head->next;
return head;
}
for( ;p->next != NULL ; p = p->next){
q = q->next;
}
p = q->next;
q->next = q->next->next;
free(p);
return head;
}
//code 2
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
struct ListNode ret, *p, *q;//设置一个虚拟头节点ret,以及两个指针
ret.next = head;
p = q = &ret;//p初始化为首地址,p从虚拟头节点往后走
while (n--) q = q->next;
q = q->next;//当前q 走到当前位置前一个节点
while (q) {
p = p->next;
q = q->next;
}
q = p->next;
p->next = q->next;
free(q);
return ret.next;
}