-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_19_RemoveNthNodeFromEndOfList.cpp
178 lines (138 loc) · 3.71 KB
/
LC_19_RemoveNthNodeFromEndOfList.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
19. Remove Nth Node From End of List
https://practice.geeksforgeeks.org/problems/nth-node-from-end-of-linked-list/1/#
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
// ListNode* removeNthFromEnd(ListNode* head, int n) {
// int len =0 ;
// ListNode* ptr = head;
// while(ptr!=nullptr)
// {
// len++;
// ptr = ptr->next;
// }
// if(len < n) return nullptr;
// ptr = head;
// ListNode* prev = nullptr;
// for(int i=1; i<len-n+1; i++)
// {
// prev = ptr;
// ptr=ptr->next;
// }
// if(prev == nullptr)
// head = head->next; // starting node
// else
// prev->next = ptr->next; // mid node
// // delete(ptr);
// // ptr = prev = nullptr;
// return head;
// }//end
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* main_ptr = head;
ListNode* ref_ptr = head;
ListNode* prev = nullptr;
if(!head) return nullptr;
for(int i=1; i<n ; i++)
{
ref_ptr = ref_ptr->next;
}
while(ref_ptr!=nullptr && ref_ptr->next !=nullptr)
{
ref_ptr = ref_ptr->next;
prev = main_ptr;
main_ptr = main_ptr->next;
}
if(prev == nullptr)
head = head->next; // starting node
else
prev->next = main_ptr->next; // mid node
return head;
}//end
};
// { Driver Code Starts
// C program to find n'th Node in linked list
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
/* Link list Node */
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
/* Function to get the nth node from the last of a linked list*/
int getNthFromLast(struct Node* head, int n);
/* Driver program to test above function*/
int main()
{
int T,i,n,l,k;
cin>>T;
while(T--){
struct Node *head = NULL, *tail = NULL;
cin>>n>>k;
int firstdata;
cin>>firstdata;
head = new Node(firstdata);
tail = head;
for(i=1;i<n;i++)
{
cin>>l;
tail->next = new Node(l);
tail = tail->next;
}
cout<<getNthFromLast(head, k)<<endl;
}
return 0;
}// } Driver Code Ends
/* struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
*/
//GFG
//Function to find the data of nth node from the end of a linked list.
int getNthFromLast(Node *head, int n)
{
Node* main_ptr = head;
Node* ref_ptr = head;
Node* prev = nullptr;
if(!head) return -1;
for(int i=1; i<n ; i++)
{
ref_ptr = ref_ptr->next;
if(!ref_ptr) return -1;
}
// int c=1;
while(ref_ptr!=nullptr && ref_ptr->next !=nullptr)
{
ref_ptr = ref_ptr->next;
prev = main_ptr;
main_ptr = main_ptr->next;
// c++;
}
// if(prev == nullptr)
// head = head->next; // starting node
// else
// prev->next = main_ptr->next; // mid node
return main_ptr->data;
}