-
Notifications
You must be signed in to change notification settings - Fork 47
/
LinkedListNthFromEnd.cpp
96 lines (77 loc) · 1.94 KB
/
LinkedListNthFromEnd.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>
using namespace std;
/* creating a node structure in C/C++ */
struct Node {
int data;
struct Node *next;
};
/* defining head as null as there is no node in the list initially. */
struct Node* head = NULL;
void push(int data) {
Node *temp;
temp = head;
/* creating a node and assigning data to it and make its next as null */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
/* check if it's empty make new_node as head */
if(head == NULL) {
head = new_node;
}
/* otherwise move upto the last and then connect last node with the new_node */
else {
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
}
/* Utility function to find the nth Node from the end. */
void nthNodeFromEnd(int nthNode) {
Node *prev, *curr;
prev = head;
curr = head;
if(head == NULL) {
cout << "The list doesn't exist." << endl;
return;
}
/* curr pointer is being moved ahead n times. */
for(int i = 0; i < nthNode; i++) {
if(curr != NULL) {
curr = curr->next;
}
else {
cout << "Enough nodes are not present in the linked list." << endl;
return;
}
}
// Now the difference between prev and curr pointer is n
/* now we'll move prev and curr pointer both until curr becomes null and finally prev will be at n from last */
while(curr != NULL) {
curr = curr->next;
prev = prev->next;
}
/* finally printing data of nth node from last */
cout << prev->data << endl;
}
/* for printing a ist */
void printList() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" "; // use printf() for C
ptr = ptr->next;
}
cout<<endl;
}
int main() {
push(2);
push(3);
push(4);
push(5);
push(6);
cout<<"Original List is: ";
printList();
nthNodeFromEnd(3);
return 0;
}