-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q19.cpp
96 lines (82 loc) · 1.69 KB
/
Q19.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
#include<iostream>
#include<vector>
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_1(ListNode* head, int n) {
int idx = 0;
vector<ListNode*> nodes;
ListNode* temp = head;
while (temp){
nodes.push_back(temp);
temp = temp->next;
}
int list_len = nodes.size();
if (list_len == 1) return NULL;
else if (list_len == 2){
if (n == 1){
delete head->next;
head->next = NULL;
return head;
}
else{
ListNode* temp = head;
head = head->next;
delete temp;
return head;
}
}
if (list_len == n){
ListNode* temp = head;
head = head->next;
delete temp;
return head;
}
ListNode* q = nodes[list_len-n-1];
ListNode* p = q->next;
q->next = p->next;
delete p;
return head;
}
ListNode* removeNthFromEnd(ListNode* head, int n){
ListNode *feakhead;
feakhead = new ListNode(-1);
feakhead->next = head;
head = feakhead;
ListNode *p1, *p2;
p1 = p2 = head;
for (int i = 0; i < n; i++){
p1 = p1->next;
}
while (p1->next){
p1 = p1->next;
p2 = p2->next;
}
ListNode* temp;
temp = p2->next;
p2->next = p2->next->next;
delete temp;
return head->next;
}
};
int main(void){
ListNode *head, *curr;
int n = 1;
head = new ListNode(1);
//curr = head;
//for (int i = 1; i < 3; i++){
// ListNode* temp;
// temp = new ListNode(i+1);
// curr->next = temp;
// curr = curr->next;
//}
Solution model;
ListNode* result = model.removeNthFromEnd(head, n);
return 0;
}