GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 184 of 203    Print  
Reverse A Linked List
What is the most efficient way to reverse a linklist?


  
Total Answers and Comments: 7 Last Update: April 25, 2009     Asked by: maninmist 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 26, 2008 23:38:50   #1  
kreach Member Since: October 2008   Contribution: 1    

RE: Reverse A Linked List
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;

};


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
December 04, 2008 04:13:33   #2  
montu Member Since: December 2008   Contribution: 2    

RE: Reverse A Linked List

//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;

}


 
Is this answer useful? Yes | No
December 29, 2008 00:51:55   #3  
maindepavan Member Since: December 2008   Contribution: 1    

RE: Reverse A Linked List

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;
}


 
Is this answer useful? Yes | No
January 07, 2009 06:41:19   #4  
B.K.Unnithan Member Since: November 2008   Contribution: 1    

RE: Reverse A Linked List
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;
}
///////////////////////////////////////////////////

 
Is this answer useful? Yes | No
February 12, 2009 21:59:19   #5  
prasoonthegreat Member Since: February 2009   Contribution: 1    

RE: Reverse A Linked List
node * rev(node *p)
{
node *q *r *s;

r NULL;
q p;

while(q! NULL)
{
s r;
q q;
q q->next;
r->next s;
}

p r;

return p;

 
Is this answer useful? Yes | No
February 28, 2009 18:54:38   #6  
dlarsson Member Since: February 2009   Contribution: 1    

RE: Reverse A Linked List
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

 
Is this answer useful? Yes | No
April 25, 2009 13:18:37   #7  
shantanu_hbd Member Since: April 2009   Contribution: 3    

RE: Reverse A Linked List

#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();
}


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape