Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions move element to the front in linked list
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <bits/stdc++.h>
using namespace std;


class Node {
public:
int data;
Node* next;
};


void moveToFront(Node** head_ref)
{

if (*head_ref == NULL || (*head_ref)->next == NULL)
return;


Node* secLast = NULL;
Node* last = *head_ref;


while (last->next != NULL) {
secLast = last;
last = last->next;
}


secLast->next = NULL;

last->next = *head_ref;


*head_ref = last;
}


void push(Node** head_ref, int new_data)
{

Node* new_node = new Node();


new_node->data = new_data;


new_node->next = (*head_ref);


(*head_ref) = new_node;
}


void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}


int main()
{
Node* start = NULL;


push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);

cout << "Linked list before moving last to front\n";
printList(start);


moveToFront(&start);

cout << "\nLinked list after removing last to front\n";
printList(start);

return 0;
}