-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove_last_Node.cpp
More file actions
55 lines (46 loc) · 975 Bytes
/
Move_last_Node.cpp
File metadata and controls
55 lines (46 loc) · 975 Bytes
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
/*
Write a function that moves the last element to the front in a given Singly Linked List. For example,
if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4.
*/
#include <iostream>
using namespace std;
struct Node{
int data;
Node *next;
Node(int data){
this->data = data;
this->next = NULL;
}
};
void moveLastNode(Node *root){
if(root==NULL){
return;
}
Node *cur = root;
Node *prev = NULL;
while(cur->next){
cout<<cur->data<<"->";
prev = cur;
cur = cur->next;
}
cout<<endl;
prev->next = NULL;
cur->next = root;
root = cur;
// cout<<prev->data<<" "<<cur->data<<endl;
cur = root;
while(cur){
cout<<cur->data<<"->";
cur = cur->next;
}
}
int main() {
// your code goes here
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
moveLastNode(head);
return 0;
}