Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse of linked list #335

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
72 changes: 72 additions & 0 deletions LinkedLists/ReverseLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//Program to reverse of linked list using "ITERATIVE APPROACH".
#include <iostream>
using namespace std;

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

//Function for traversing the linked list,
Node *traverse(Node *head)
{
Node *ptr = head;
while (ptr != NULL)
{
cout << ptr->data << " ";
ptr = ptr->next;
}
return head;
}

//Function for reverse the nodes in linked list
Node *reverse(Node *head)
{
Node *prevNode;
Node *currentNode;
Node *nextNode;
prevNode = NULL;

currentNode = nextNode = head; //Initially our these pointer is at starting same as head.

while (nextNode != NULL)
{
nextNode = nextNode->next;
currentNode->next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
head = prevNode;
return head;
}

int main()
{
Node *head = NULL;
Node *n2 = NULL;
Node *n3 = NULL;
Node *n4 = NULL;

head = new Node();
n2 = new Node();
n3 = new Node();
n4 = new Node();

head->data = 34;
head->next = n2;

n2->data = 37;
n2->next = n3;

n3->data = 56;
n3->next = n4;

n4->data = 9;
n4->next = NULL;

head = reverse(head);
traverse(head);
return 0;
}