Reverse A Linked List

What is the most efficient way to reverse a linklist?

Questions by maninmist

Showing Answers 1 - 22 of 22 Answers

kreach

  • Oct 26th, 2008
 

How about this?



template<class T>
class List {
private:

template<class T>
struct Node {
Node() : next(NULL)
{
}

T value;
Node* next;
};

public:
List() : root(NULL), current(NULL)
{
}

~List()
{
Node<T>* node = root;

while(node != NULL) {
Node<T>* temp = node->next;
delete node;
node = temp;
}
}

void Add(T value)
{
if(current != NULL) {
current->next = new Node<T>;
current->next->value = value;
current = current->next;
}
else {
root = new Node<T>;
root->value = value;
current = root;
}
}

void Reverse()
{
Node<T> *node1 = NULL, *node2 = NULL, *node3 = NULL;

node1 = root;

if(node1 != NULL) {
node2 = node1->next;
}

if(node2 != NULL) {
node3 = node2->next;
}

while(node1 != NULL && node2 != NULL) {
node2->next = node1;

node1 = node2;
node2 = node3;

if(node3 != NULL) {
node3 = node3->next;
}
}

if(root != NULL) {
root->next = NULL;
}

current = root;
root = node1;
}

void Print() const
{
Node<T>* next = root;

while(next != NULL) {
cout << next->value << endl;
next = next->next;
}
}

private:
Node<T>* root;
Node<T>* current;

};

  Was this answer useful?  Yes

montu

  • Dec 4th, 2008
 

//Reverse the Link List using recursion
struct node *head = NULL;
void reverse(struct node *parent, struct node *ptr)
{
    if(ptr == NULL)
        return;
    if(ptr->next != NULL)
    {
        reverse(ptr,ptr->next);
    }
    else
    {
        head = ptr;
    }

    ptr->next=parent;

}

  Was this answer useful?  Yes

maindepavan

  • Dec 29th, 2008
 

function revLL(structNode **LL)
{
     structNode *t1,t2,t3;
     t1=*LL;
     t2=t3=NULL;
     while(t1!NULL)
     {

          t3=t2;

          t2=t1;

          t1=t1->next;

          t2->Next=t3;

     }
     *LL=t2;
}

  Was this answer useful?  Yes

struct node
{
    int iData;
    struct node *iNext;
};

typedef struct node Node;

///////////////////////////////////////////////////
Node * ReverseList(Node *aHead)
{
    Node *prev;
    Node *cur;

    if(aHead == NULL || aHead->iNext == NULL)
    {
        return aHead;
    }

    prev = aHead;
    cur = aHead->iNext;

    aHead = ReverseList(cur);

    cur->iNext = prev;
    prev->iNext = NULL;

    return aHead;
}
///////////////////////////////////////////////////

  Was this answer useful?  Yes

dlarsson

  • Feb 28th, 2009
 

If this is a doubly linked list, you probably have "head" and "tail" pointers. I would just swap those pointers, giving you a O(1) complexity:

listNode * temp = list.head;
list.head = list.tail;
list.tail = temp;
temp = NULL;

=D

  Was this answer useful?  Yes

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class Node
{
 public:
  int data;
  Node(int data);
  Node *next;
  void display();
};
Node::Node(int data)
{
 this->data=data;
}
void Node::display()
{
 cout<<data<<" ";
}
class List
{
 public:
  Node* start;
  List(){start=NULL;}
  void add(Node *node);
  void display();
  void reverse();
};
void List::reverse()
{
 //a b and c points to 3 successive elements
 Node *a=NULL,*b=NULL,*c=start;

 while(c!=NULL)
 {
  //all of these pointers proceed one step ahead
  a = b;
  b = c;
  c = c->next;

  if(b!=NULL)// b has entered the list
  {
    b->next=a;
  }
  //when the above loop was broken last element was pointing to second last
  //let start now point to last element
  start=b;
}

}
void List::add(Node * node)
{
  node->next=start;
  start=node;
}
void List::display()
{
  Node *ptr=start;
  while(ptr!=NULL)
  {
   ptr->display();
   ptr=ptr->next;
  }
  cout<<endl;
}
void main()
{
 clrscr();
 List list ;
 list.add(new Node(34));
 list.add(new Node(23));
 list.add(new Node(12));
 list.add(new Node(8));
 list.display();
 list.reverse();
 list.display();
 getch();
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions