-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove Nth Node From End of List.cpp
91 lines (71 loc) · 1.55 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
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
/*
Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
For 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.
Try to do this in one pass.
*/
#include <iostream>
using namespace std;
//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) {
ListNode *p, *q;
p = q = head;
for (int i = 0; i < n; i++)
{
q = q->next;
}
if (q == NULL){
return p->next;
}
while (q->next != NULL){
p = p->next;
q = q->next;
}
ListNode* temp = p->next;
p->next = temp->next;
delete(temp);
return head;
}
};
ListNode* ListAdd(ListNode* head, ListNode* node){
if (head == NULL)return NULL;
ListNode* p = head;
while (p->next != NULL){
p = p->next;
}
p->next = node;
return head;
}
void PrintList(ListNode* head){
ListNode* p = head;
if (head == NULL) return;
while (p != NULL){
cout << p->val << "->";
p = p->next;
}
cout << "NULL" << endl;
}
void main()
{
Solution mySolu;
ListNode* Head = new ListNode(1);
ListNode* Node1 = new ListNode(2);
ListNode* Node2 = new ListNode(3);
ListNode* Node3 = new ListNode(4);
ListNode* Node4 = new ListNode(5);
ListAdd(Head, ListAdd(Node1, ListAdd(Node2, ListAdd(Node3, Node4))));
PrintList(Head);
mySolu.removeNthFromEnd(Head,2);
PrintList(Head);
}