-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remove Nth Node From End of List.cpp
62 lines (59 loc) · 1.42 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
58
59
60
61
62
/**
* 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) {
if(head==NULL||n<0)//就是处理特殊情况
{
return head;
}
ListNode* p=head->next;
int m=1;
while(p!=NULL)
{
p=p->next;
m++;
}
if(m<n)//处理输入进来的数超过了链表的长度
{
return head;
}
ListNode* pNode1=head;
ListNode* pNode2=head;
int i=1;
if(i==n&&pNode2->next!=NULL)//表明就是尾结点了;且不止一个结点。
{
while(pNode2->next!=NULL)
{
pNode1=pNode2;
pNode2=pNode2->next;
}
pNode1->next=pNode2->next;
return head;
}
while(i<=n&&pNode2!=NULL)
{
pNode2=pNode2->next;
i++;
}
if(pNode2==NULL)//这个是处理删除头结点
{
head=head->next;
return head;
}
while(pNode2!=NULL)
{
pNode1=pNode1->next;
pNode2=pNode2->next;
}
pNode1->val=pNode1->next->val;
pNode1->next=pNode1->next->next;
return head;
}
};