Skip to content

Commit fe369b7

Browse files
committed
Completed Linked List - 1
1 parent 45f86dd commit fe369b7

File tree

9 files changed

+1201
-0
lines changed

9 files changed

+1201
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
Title: Append Last N to First
3+
4+
Problem statement
5+
You have been given a singly linked list of integers along with an integer 'N'. Write a function to
6+
append the last 'N' nodes towards the front of the singly linked list and returns the new head to the list.
7+
8+
Hint:
9+
Identify how many pointers you require and try traversing them to right places and connect nodes accordingly
10+
also don't forget to disconnect what's required else it could create cycles.
11+
12+
Detailed explanation ( Input/output format, Notes, Images )
13+
Input format :
14+
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the
15+
test cases follow.
16+
The first line of each test case or query contains the elements of the singly linked list separated by a single space.
17+
The second line contains the integer value 'N'. It denotes the number of nodes to be moved from last to the front of the singly linked list.
18+
19+
Output format :
20+
For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space.
21+
Output for every test case will be printed in a seperate line.
22+
23+
Constraints :
24+
1 <= t <= 10^2
25+
0 <= M <= 10^5
26+
0 <= N < M
27+
28+
Time Limit: 1sec
29+
Where 'M' is the size of the singly linked list.
30+
31+
Remember/Consider :
32+
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.
33+
34+
Sample Input 1 :
35+
2
36+
1 2 3 4 5 -1
37+
3
38+
10 20 30 40 50 60 -1
39+
5
40+
41+
Sample Output 1 :
42+
3 4 5 1 2
43+
20 30 40 50 60 10
44+
45+
Sample Input 2 :
46+
1
47+
10 6 77 90 61 67 100 -1
48+
4
49+
50+
Sample Output 2 :
51+
90 61 67 100 10 6 77
52+
53+
Explanation to Sample Input 2 :
54+
We have been required to move the last 4 nodes to the front of the list. Here, "90->61->67->100" is
55+
the list which represents the last 4 nodes. When we move this list to the front then the remaining
56+
part of the initial list which is, "10->6->77" is attached after 100. Hence, the new list formed
57+
with an updated head pointing to 90.
58+
*/
59+
60+
#include<iostream>
61+
using namespace std;
62+
63+
class Node {
64+
public:
65+
int data;
66+
Node *next;
67+
Node(int data) {
68+
this->data = data;
69+
this->next = NULL;
70+
}
71+
};
72+
73+
Node * appendLastNToFirst(Node * head, int n) {
74+
if(head == NULL) {
75+
return NULL;
76+
}
77+
78+
int count = 0;
79+
Node * tail = NULL;
80+
Node * current = head;
81+
while(current != NULL) {
82+
count++;
83+
tail = current;
84+
current = current->next;
85+
}
86+
87+
int pos = count - n;
88+
int idx = 1;
89+
Node * temp = head;
90+
while(temp != NULL && idx < pos) {
91+
idx++;
92+
temp = temp->next;
93+
}
94+
95+
tail->next = head;
96+
head = temp->next;
97+
temp->next = NULL;
98+
return head;
99+
}
100+
101+
Node *takeinput() {
102+
int data;
103+
cin >> data;
104+
Node *head = NULL, *tail = NULL;
105+
while (data != -1) {
106+
Node *newnode = new Node(data);
107+
if (head == NULL) {
108+
head = newnode;
109+
tail = newnode;
110+
} else {
111+
tail->next = newnode;
112+
tail = newnode;
113+
}
114+
cin >> data;
115+
}
116+
return head;
117+
}
118+
119+
void print(Node *head) {
120+
Node *temp = head;
121+
while (temp != NULL) {
122+
cout << temp->data << " ";
123+
temp = temp->next;
124+
}
125+
cout << endl;
126+
}
127+
128+
int main() {
129+
int t;
130+
cin >> t;
131+
while (t--) {
132+
Node *head = takeinput();
133+
int n;
134+
cin >> n;
135+
head = appendLastNToFirst(head, n);
136+
print(head);
137+
}
138+
return 0;
139+
}

F25LinkedList-1/DeleteNodeInLL.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
Title: Delete a node from Linked List
3+
4+
Problem statement
5+
You have been given a linked list of integers. Your task is to write a function that deletes a node
6+
from a given position, 'POS'.
7+
8+
Note :
9+
Assume that the Indexing for the linked list always starts from 0.
10+
If the position is greater than or equal to the length of the linked list, you should return the
11+
same linked list without any change.
12+
13+
Illustration :
14+
The following images depict how the deletion has been performed.
15+
Image-I :
16+
Head -> 10 -> 20 -> 30 -> 40 -> 50 -> NULL
17+
18+
Image-II :
19+
Head -> 10 -> 20 -> 40 -> 50 -> NULL
20+
30 ->|
21+
Head -> 10 -> 20 -> 40 -> 50 -> NULL
22+
23+
Detailed explanation ( Input/output format, Notes, Images )
24+
Input format :
25+
The first line contains the elements of the linked list separated by a single space.
26+
The second line contains the integer value of 'POS'. It denotes the position in the linked list from
27+
where the node has to be deleted.
28+
29+
Remember/Consider :
30+
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence,
31+
would never be a list element
32+
33+
Output format :
34+
Print the resulting linked list of integers in a row, separated by a single space.
35+
Note:
36+
37+
You are not required to print the output, it has already been taken care of. Just implement the function.
38+
39+
Sample Input 1 :
40+
3 4 5 2 6 1 9 -1
41+
3
42+
43+
Sample Output 1 :
44+
3 4 5 6 1 9
45+
46+
Explanation of Sample Output 1 :
47+
The data in the node with index 3 is 2 which has been removed.
48+
49+
Sample Input 2 :
50+
3 4 5 2 6 1 9 -1
51+
0
52+
53+
Sample Output 2 :
54+
4 5 2 6 1 9
55+
56+
Constraints :
57+
0 <= N <= 10^5
58+
POS >= 0
59+
60+
Time Limit: 1sec
61+
Expected Complexity :
62+
Time Complexity : O(N)
63+
Space Complexity : O(1)
64+
*/
65+
66+
#include<iostream>
67+
#include<fstream>
68+
#include<vector>
69+
#include<cmath>
70+
#include<algorithm>
71+
#include<bits/stdc++.h>
72+
using namespace std;
73+
74+
class Node {
75+
public:
76+
int data;
77+
Node *next;
78+
Node(int data) {
79+
this->data = data;
80+
this->next = NULL;
81+
}
82+
};
83+
84+
Node *deleteNode(Node *head, int pos) {
85+
// Write your code here.
86+
if(head == NULL) {
87+
return NULL;
88+
}
89+
90+
int idx = 0;
91+
Node * prev = NULL;
92+
Node * current = head;
93+
94+
while(current != NULL && idx < pos) {
95+
idx++;
96+
prev = current;
97+
current = current->next;
98+
}
99+
100+
if(pos == 0) {
101+
return head->next;
102+
}
103+
104+
if(idx == pos) {
105+
prev->next = current->next;
106+
}
107+
108+
return head;
109+
}
110+
111+
112+
class Runner {
113+
int pos;
114+
Node *head = NULL;
115+
public:
116+
117+
void takeInput() {
118+
int data;
119+
cin >> data;
120+
Node *tail = NULL;
121+
while (data != -1) {
122+
Node *newNode = new Node(data);
123+
if (head == NULL) {
124+
head = newNode;
125+
tail = newNode;
126+
} else {
127+
tail->next = newNode;
128+
tail = newNode;
129+
}
130+
cin >> data;
131+
}
132+
cin >> pos;
133+
}
134+
135+
Node * copyll(Node *chead) {
136+
if(chead == NULL)return NULL;
137+
Node *nhead= NULL;
138+
139+
Node *tail = NULL;
140+
while (chead != NULL) {
141+
Node *newNode = new Node(chead->data);
142+
if (nhead == NULL) {
143+
nhead = newNode;
144+
tail = newNode;
145+
} else {
146+
tail->next = newNode;
147+
tail = newNode;
148+
}
149+
chead=chead->next;
150+
}
151+
return nhead;
152+
}
153+
154+
void print(Node *head) {
155+
Node *temp = head;
156+
while (temp != NULL) {
157+
cout << temp->data << " ";
158+
temp = temp->next;
159+
}
160+
cout << endl;
161+
}
162+
163+
void execute() {
164+
Node *copyhead = copyll(head);
165+
copyhead = deleteNode(copyhead, pos);
166+
while(copyhead!=NULL) {
167+
Node* temp=copyhead;
168+
copyhead=copyhead->next;
169+
delete temp;
170+
}
171+
}
172+
173+
void executeAndPrintOutput() {
174+
Node *copyhead = copyll(head);
175+
copyhead = deleteNode(copyhead, pos);
176+
print(copyhead);
177+
while(copyhead!=NULL) {
178+
Node* temp=copyhead;
179+
copyhead=copyhead->next;
180+
delete temp;
181+
}
182+
}
183+
};
184+
185+
int main() {
186+
Runner runner;
187+
runner.takeInput();
188+
runner.executeAndPrintOutput();
189+
return 0;
190+
}

0 commit comments

Comments
 (0)