/
19.remove-nth-node-from-end-of-list.cpp
106 lines (94 loc) · 2.29 KB
/
19.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* @lc app=leetcode id=19 lang=cpp
*
* [19] Remove Nth Node From End of List
*
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
*
* algorithms
* Medium (33.99%)
* Total Accepted: 359K
* Total Submissions: 1.1M
* Testcase Example: '[1,2,3,4,5]\n2'
*
* Given a linked list, remove the n-th node from the end of list and return
* its head.
*
* Example:
*
*
* Given linked list: 1->2->3->4->5, and n = 2.
*
* After removing the second node from the end, the linked list becomes
* 1->2->3->5.
*
*
* Note:
*
* Given n will always be valid.
*
* Follow up:
*
* Could you do this in one pass?
*
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// 思路:
// 首先双指针,找到要删除的节点的上一个节点
// 然后再 删除要删除的节点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
// 0. 倒数第零个没有意义
if (head == NULL || n == 0) {
return NULL;
}
ListNode* prior = head;
ListNode* behind = head;
// 1. 先走几步(第二个指针需要)
for(int i = 1; i <= n-1; i++)
{
if(prior->next != NULL)
{
prior = prior->next;
}else
{
return NULL; //根本就没那么多步
}
}
// 1.1 要是走到头了,就说明删除的是头节点
if(prior->next == NULL)
{
ListNode* newHead = head->next;
delete head;
head = newHead;
return head;
}
prior = prior->next;
// 2. 一起走
while(prior->next != NULL){
prior = prior->next;
behind = behind->next;
}
// 3. 删除下一个节点
// 如果要删的是尾节点的话
if (behind->next->next == NULL) {
delete behind->next;
behind->next = NULL;
}else
{
ListNode* copy = behind->next;
behind->next = behind->next->next;
delete copy;
copy = NULL;
}
return head;
}
};