Internet RFC Index
Usenet FAQ Index
Other FAQs
Documents
Tools
Search
Search FAQs
Search RFCs
Cities
Countries
Hospitals
Web Hosting Ratings
[
Home
|
FAQ-Related Q&As
|
General Q&As
|
Answered Questions
]
Search the Q&A Archives
...most efficient method to reverse a linked list ?...
<< Back to: comp.lang.c FAQ list Table of Contents
Question by Shashikiran
Submitted on 12/10/2003
Related FAQ:
comp.lang.c FAQ list Table of Contents
Rating:
Rate this question:
N/A
Worst
Weak
OK
Good
Great
What is the most efficient method to reverse a linked list ?
Answer by Sudhakar
Submitted on 1/2/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
swap first elment and last element.
Then second element with second element from last and proceed like tht..
Answer by ricki
Submitted on 2/17/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Recursive method of reversing the linked list is the best bet !
Answer by raghu
Submitted on 3/2/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Sudhakar,
singly linked list??
if its a doubly linked list then just swap the prev and next pointers for each node and make the current tail as head.
else ricki'sanswer would be fine.
Answer by spongman
Submitted on 3/5/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
You don't need recursion to reverse a singly-linked list.
Node *Reverse (Node *p)
{
Node *pr = NULL;
while (p != NULL)
{
Node *tmp = p->next;
p->next = pr;
pr = p;
p = tmp;
}
return pr;
}
Answer by rana
Submitted on 3/9/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
can we reverse a singly linked list using just one variable ?
Answer by Peter
Submitted on 3/21/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Yes, you can.
You have to keep the pointer to the first element in a variable, go to the second and swap the values, then go to the third and keep doing this.
Do you know how to swap values without a temporary variable?
a = 2;
b = 3;
a = a + b; (now a is 5)
b = a - b; (now b is 2)
a = a - b; (now a is 3)
pplupo@hotpop.com
Answer by DJ
Submitted on 3/25/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Can someone show me the code for linked list reversal using recursion when the argument passed is only the head.
thanx
Answer by bijusown
Submitted on 4/5/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Node *reverseList(Node *current, Node *parent)
{
Node *revHead;
if(current == null)
revHead = parent;
else
{
revHead = reverseList(current->next,current);
current->next = parent;
}
return revHead;
}
Initial method call should be
head = reverseList(head,NULL)
Answer by murtuzabookwala
Submitted on 4/22/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Unfortunately uses global variable 'tail'
which would later point to the reversed list
list *tail;
list *reverse (list *node)
{
if (node->next == NULL)
{
tail = node;
return;
}
else
{
reverse (node->next);
node->next->next = node;
node->next = NULL;
}
}
Called as: reverse(head);
Answer by bob
Submitted on 4/24/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Recursion is noones friend
Answer by mozzis
Submitted on 4/30/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
bijusown's answer destroys the list without doing anything useful. For instance, the first time through the loop, the head's next pointer is replaced with NULL. Oops - now you've orphaned the rest of the list.
Answer by nelly
Submitted on 5/5/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
mozzis, actually, that's not true. In bijusown'ssolution, the initial head's next pointer is only set to null after the rest of the list has been reversed. Hence, the old head now becomes the new tail while the old head->next is now pointing to head.
Answer by nelly
Submitted on 5/5/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
mozzis, actually, that's not true. In bijusown's solution, the initial head's next pointer is only set to null after the rest of the list has been reversed. Hence, the old head now becomes the new tail while the old head->next is now pointing to head.
Answer by hanpa00
Submitted on 5/10/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Another recursive function that can reverse a linked list.
To call the function:
newHead = reverse(head);
struct Node *reverse(Node *curp)
{
static struct Node *head = curp;
static struct Node *revHead = NULL;
if (curp == NULL)
return NULL;
if (curp->next == NULL)
revHead = curp;
else
reverse(curp->next)->next = curp;
if (curp == head) {
curp->next = NULL;
return revHead;
}
else
return curp;
}
Answer by am
Submitted on 5/11/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Is there a particular reason why someone would want to implement this recursively, as opposed to the iterative solution? Just wondering... Unless the recursive case is for interviewing purposes to show familiarity with the concept?
Answer by al
Submitted on 5/20/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Am, what else is recursion for?
Answer by sdfsd
Submitted on 6/23/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
sdfsdfsdf
Answer by ramuguda
Submitted on 6/27/2004
Rating:
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
node *reverse(node *head)
{
node *temp;
if(head->next)
{
temp=reverse(head->next);
head>next->next=head;
head->next=NULL;
}
else
temp=head;
return temp;
}
call as: head=reverse(head);
Answer by basha nit Warangal
Submitted on 8/20/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
struct node * reverse(struct node *n)
{
if (n->next==NULL)
{
return n;
}
return (reverse(first,n->next)->next=n);
}
here function call is like
first = reverse(first)where first is a pointer to starting node.
Answer by darathi
Submitted on 8/25/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
we can reverse the single linked list using stack or recursive. which one is best in these two
Answer by Kest
Submitted on 9/10/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
The simplest recursive solution IMHO is:
Node * reverse(Node * cur, Node * par) {
Node * temp = cur->next;
cur->next = par;
if (temp == NULL)
return cur;
else
return reverse(temp,cur);
}
Answer by Shantanu
Submitted on 9/16/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Suppose you already have the head and tail of the list.
Node* Reverse( )
{
Node * NewHead = Head;
Node * AuxiliaryTail = Tail;
While ( NewHead != AuxiliaryTail )
{
Node* Tmp = NewHead ;
NewHead = NewHead->next ;
Tmp->next = AuxiliaryTail->next;
AuxiliaryTail->next = Tmp;
}
return NewHead;
}
Answer by gaur
Submitted on 10/5/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
bijusown'ssolution is not correct. While reversing the linked list only the last link in the list is returned and the rest of the linked list is isolated
Answer by Sandeep.D.M
Submitted on 11/20/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void reverse(Node *First)
{
Node *Sec,*Thir; //Create 2nd &3rd nextnode
Sec=First->next;
First->next=NULL;
while(Thir != NULL)
{
Thir=Sec->next; //get the seconf & third
s->next=First; //node address
First=Sec;
Sec=Thir;
Thir=Thir->next;
}
Sec->next=First;
return (Sec); //Return the first node
}
Answer by basha
Submitted on 12/22/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
struct node * reverse(struct node **first,struct node *h)
{
if (h->next == NULL)
{
*first=h;
return h;
}
else
return(reverse(h->next)->next=h);
}
suppose
struct node
{
int data;
struct node *next;
}*first;
Here first is the pointer to starting node of the single linked list.
Function call should be
reverse(&first,first);
Answer by BASHA
Submitted on 12/23/2004
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
REVERSING A LINKED LIST
struct node * reverse(struct node **first, struct node *h)
{
if (h && h->next ==NULL)
{
*first=h;
return h;
}
else if (h)
return ( reverse(&first,h->next)->next = h);
return NULL;
}
NODE STRUCTURE IS
struct node
{
int data;
struct node *next;
}*first=NULL:
HERE first IS A POINTER TO STARTING NODE OF THE SINGLE LINKED LIST.
CALL TO THIS FUNCTION IS
reverse(&first,first);
Answer by wkc
Submitted on 1/9/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
the "if" should be "if (head && head->next), in case the head parameter is NULL.
but otherwise the solution is elegant.
Answer by Nealo
Submitted on 2/2/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Recursion is only good for helping programmers think. Anything done with recursion can be done more efficiently without it, because you don't have to waste time calling functions. For recursively reversing a list, this would be a function call for each element. But programming ease has become a high priority now that processing power is fairly cheap.
Answer by Nealo
Submitted on 2/2/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Also, for swapping two values without a temp variable, using binary XOR may be a better idea, because you don't have to worry about overflow. Though I don't remember if you can XOR in C, you should probably watch out for overflow.
Answer by Al
Submitted on 2/16/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void reverse(list *head) {
list *temp1, *temp2;
if (head == null)
return;
temp1 = head->next;
head->next = null;
while(temp1 != null) {
temp2 = temp1->next;
temp1-> next = head;
head = temp1;
temp1 = temp2;
}
}
Answer by Krishna Nadh
Submitted on 2/18/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
// Reverses the given linked list by changing its .next pointers and
// its head pointer. Takes a pointer (reference) to the head pointer.
void Reverse(struct node** headRef) {
if (*headRef != NULL) { // special case: skip the empty list
/*
Plan for this loop: move three pointers: front, middle, back
down the list in order. Middle is the main pointer running
down the list. Front leads it and Back trails it.
For each step, reverse the middle pointer and then advance all
three to get the next node.
*/
struct node* middle = *headRef; // the main pointer
struct node* front = middle->next; // the two other pointers (NULL ok)
struct node* back = NULL;
while (1) {
middle->next = back; // fix the middle node
if (front == NULL) break; // test if done
34
back = middle; // advance the three pointers
middle = front;
front = front->next;
}
*headRef = middle; // fix the head pointer to point to the new front
}
}
Answer by Krishna Nadh
Submitted on 2/18/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
// Reverses the given linked list by changing its .next pointers and
// its head pointer. Takes a pointer (reference) to the head pointer.
void Reverse(struct node** headRef) {
if (*headRef != NULL) { // special case: skip the empty list
/*
Plan for this loop: move three pointers: front, middle, back
down the list in order. Middle is the main pointer running
down the list. Front leads it and Back trails it.
For each step, reverse the middle pointer and then advance all
three to get the next node.
*/
struct node* middle = *headRef; // the main pointer
struct node* front = middle->next; // the two other pointers (NULL ok)
struct node* back = NULL;
while (1) {
middle->next = back; // fix the middle node
if (front == NULL) break; // test if done
34
back = middle; // advance the three pointers
middle = front;
front = front->next;
}
*headRef = middle; // fix the head pointer to point to the new front
}
}
Answer by IceBryce
Submitted on 2/24/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
public void reverse()
{
Node curr = top;
if(top!=null && top.next != null)
{
top = top.next;
reverse();
curr.next.next=curr;
curr.next=null;
}
} // end method reverse
Answer by Kartik
Submitted on 2/25/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
And the iterative counterpart:
node* reverse(node* now)
{
node* temp;
node* tobe = NULL;
while(temp != null)
{
temp = now->next;
now->next = tobe;
tobe = now;
now = temp;
}
return now;
}
node* new_start = reverse(orig_start);
Answer by Sohel
Submitted on 2/26/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void reverse(node **s)
{
node *back;
node *front;
node *a=*s;
int temp;
if(a==NULL) return;
if(a->next==NULL) return;
front = a;
back=NULL;
while (a != NULL)
{
front=a->next;
a->next=back;
back=a;
a=front;
}//while
*s = back;
}
Answer by priya
Submitted on 2/28/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
hi
please help me to write this code .
write a code to print out the intersection of two single linked lists in a (decending order)ie after getting the intersection elements you have to sort it in a descending order and then print it out.
Answer by priya
Submitted on 2/28/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
hi
write a code to print out the intersection of two single linked lists in a reverse order.
Answer by navneet
Submitted on 3/20/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
//
// iterative version
//
Node* ReverseList( Node ** List )
{
Node *temp1 = *List;
Node * temp2 = NULL;
Node * temp3 = NULL;
while ( temp1 )
{
*List = temp1; //set the head to last node
temp2= temp1->pNext; // save the next ptr in temp2
temp1->pNext = temp3; // change next to privous
temp3 = temp1;
temp1 = temp2;
}
return *List;
}
Answer by AshTray
Submitted on 4/4/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
This piece of code works correctly and doesn't assume any headnodes.
šUstruct node* recRev(struct node *head)
šU{
šUstruct node *temp;
šU
šU if(head -> next)
šU {
šU temp = recRev(head -> next);
šU head->next->next = head;
šU head->next=NULL;
šU }
šU else
šU temp = head;
šUreturn temp;
šU}
Answer by naik
Submitted on 4/7/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void reverse(struct doublell **dll)
{
struct doublell *a,*b,*c;
a = *dll;
c=NULL;
while(a!=NULL)
{
b=c;
c=a;
a=a->link;
c->link=b;
}
*dll = c;
}
Answer by available
Submitted on 4/22/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Recursions are beautiful and simple. However, in real world, the iterative algorithm is more robust. The simple reason is the fact that the stack in most systems has a limited size. Therefore, depending how deep your recursive function is called or how long the linked list is, you may run out of stack and ... Well, you will get a late night call to go fix it, :-(.
Answer by Max
Submitted on 5/1/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void
reverse(struct node **head)
{
if((*head == NULL)||((*head)->next==NULL))
return;
struct node *first = *head;
struct node *rest = first->next;
reverse(&rest);
first->next->next = first;
first->next = NULL;
*head = rest;
}
Answer by ub
Submitted on 5/4/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
This is (almost) the same as the solution by ramuguda - except that you don't need a temp
struct node * recreverse (struct node* n) {
if (!n->nextnode)
return n;
else {
recreverse(n->nextnode);
n->nextnode->nextnode = n;
n->nextnode = NULL;
}
}
Answer by Lolla
Submitted on 5/5/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
node* Reverse_List(node* head)
{
if((head == NULL) || (head->next) == NULL))
return head;
else{
node* p1, *p2, *tmp;
p1 = h; p2 = p1->next; temp = p2; p1->next = NULL;
while(p2->next != NULL){
temp = p2;
p2->next = p1->next;
p1 = tmp;
p2 = tmp->next;
}
h = p2;
return h;
}
}
}
Answer by bhandari
Submitted on 5/8/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
I am maintaining a global start pointer which points to the first node.
How to reverse the list. I am not passing any element to the reverse() function.
Answer by kissie
Submitted on 5/23/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
node *reversenode(node * start)
{
node *temp,*front = start;
if(front->next)
{
temp = reversenode(front->next);
temp->next = front;
temp = front;
temp->next =NULL;
}
else
temp = front;
return temp;
}
Answer by sandhya
Submitted on 5/27/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
please can any one show me a c program to create a single linked list?
thanks
Answer by quirrex
Submitted on 6/7/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Peter, could you please show me the code of how do you reverse the list with just one variable?
thanx
Answer by Danny
Submitted on 6/11/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
ramuguda, ur solution almost always returns NULL (except that the list contains only 1 element), which is incorrect.
Answer by sreenithk
Submitted on 6/28/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
another way to reverse linklist recursively. linklist is the class and head a member of that class which holds the pointer to first link of the linklist a.k.a head.
void linklist::reverse(linklist *current, linklist *parent)
{
if(current->next == NULL)
head = current;
else
reverse(current->next, current);
current->next = parent;
}
Answer by RakeshPawar
Submitted on 7/20/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Recursive Method to reverse a singly Linked List: ---
Node *Reverse(Node *ptr)
{
if(ptr->next==NULL)
return ptr;
temp=Reverse(ptr->next)
temp>next=ptr;
return ptr;
}
Answer by Soham
Submitted on 7/31/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Node* Rev(Node *pHead)
{
Node *temp = NULL;
if (pHead && pHead->pNext) //short circuit
{temp = Rev(pHead->pNext);}
else
{return pHead;}
pHead->pNext->pNext = pHead;
pHead->pNext = NULL;
return temp;
}
Answer by Soham
Submitted on 7/31/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Iterative solution:
void RevIter(Node **ppHead)
{
Node *pF = NULL, *pS = NULL, *pSave = NULL;
if(!ppHead || !*ppHead)
return;
pF = *ppHead;
pS = pF->pNext;
pF->pNext = NULL; //Last node
while(pS)
{
pSave = pS->pNext;
pS->pNext = pF;
pF = pS;
pS = pSave;
}
*ppHead = pF;
}
Answer by ram
Submitted on 8/3/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
how dos o.s can be installed
Answer by trilogy
Submitted on 8/22/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Gr8 answer guys , but the fundamental concept is the same for everyones reverse function, cheers
Answer by RAMASWAMY BM
Submitted on 10/8/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
reverse()
{
p=head;
q=p;
p->next=NULL;
while(q!=NULL)
{
r=q->next;
p->next=q;
p=q;
q=r;
}
head=p;
}
Answer by joan
Submitted on 10/25/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void reverse(node *now)
{
head = now->next;
if (now->next->next != NULL )
reverse(now->next);
if ( now->next->next == NULL ){
now->next->next = node;
now->next = NULL;
};
};
Answer by Bodaddy
Submitted on 11/10/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
Here's and iterative approach
ListItem * ReverseList(ListItem * list)
{
//Makes use of two pointers and the
//passed in list pointer to reverse
//the singly linked list
if(list)
{
ListItem * temp0 = list;
ListItem * temp1 = NULL;
while(list->next)
{
list = list->next;
temp0->next = temp1;
temp1=temp0;
temp0=list;
}
//we are at the tail node so set
//the list pointer's next to temp1
list->next = temp1;
}
return list;
}
Call as head=ReverseList(head)
Answer by asdf
Submitted on 11/16/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
poon kha lae :)
Answer by whats wrong with
Submitted on 11/25/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
public void reverse ()
{
Node a = list;
Node b = a.next;
a.next = null;
Node c = b.next;
do
{
b.next = a;
c.next = b;
a = b;
b = c;
c = c.next;
}
while (a != null);
list = a;
}
Answer by Anupam
Submitted on 11/30/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
What is better ? in terms of efficieny.. iterative method or recursive method?
Answer by Santosh P K
Submitted on 12/20/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
struct node* reverse(struct node *n1, struct node *n1Parent)
{
struct node *temp;
if (n1 == NULL)
{
return n1Parent;
}
temp = n1->next;
n1->next = n1Parent;
return reverse (temp,n1);
}
function should be called as reverse (n1, NULL);
Answer by Santosh P K
Submitted on 12/20/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
struct node *reverse (struct node *node)
{
struct node *node1;
if (node->next == NULL)
{
return node;
}
else
{
node1 = reverse1 (node->next);
node1->next = node;
node->next = NULL;
return node;
}
}
Answer by xtele
Submitted on 12/22/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
simplest one.
Node* recursive_reverse(Node *node)
{
if (!node)
return NULL;
if (!node->next) {
recursive_reverse(node->next);
node->next->next = node;
}
return node;
}
Answer by palash
Submitted on 12/28/2005
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
best efficient method is:
NODE reverse_list(NODE first)
{
NODE temp,cur;
cur=NULL;
while(first!=NULL)
{
temp=first;
first=first->link;
temp->link=cur;
cur=temp;
}
return cur;
}
Answer by Foobar
Submitted on 1/22/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void RecursiveReverse(Node ** pnHead, Node * pnCurr, Node * pnPrev)
{
assert(pnHead && *pnHead);
if (!pnCurr)
{
(*pnHead)->pnNxt = NULL;
*pnHead = pnPrev;
return;
}
pnPrev = pnCurr;
pnCurr = pnCurr->pnNxt;
RecursiveReverse(pnHead, pnCurr, pnPrev);
if (pnCurr)
pnCurr->pnNxt = pnPrev;
}
Answer by AshwathKumar_Punacha
Submitted on 1/30/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
The basic idea is to use three pointers and walk them down the list. Within the list, the 'forward' pointer is moved down one item. The 'middle' pointer it left where it is, one behind the 'forward' pointer, while the 'back' pointer is left one behind the 'middle' pointer. The 'next' field of the 'middle' pointer's object is set to point at the 'back' pointer, thus reversing the direction of the link in the middle. At this point, the 'back' pointer is moved up one by assigning it to point at 'middle'sobject, and middle is moved up one by assigning it to point at 'forward'sobject, which sets everything up for the next iteration. With that in mind, it's only necessary to take care of the special cases that arise at the beginning and end of the linked list.
Answer by asdf
Submitted on 2/5/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
who cares
Answer by xspark86
Submitted on 2/15/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
WONDERFUL ANSWER BY ramuguda.
Can anyone, though, explain how he developed this code? I have no idea. Are there mathematical/analytical way of approach to develop the above code?
Answer by MOHIT KHURANA
Submitted on 2/26/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
code for linked list reversal using recursion when the argument passed is only the head.
--------------------------------------------
void reversedisp(node *p)
{
if(p==0)
return;
else
{
return(reversedisp(p->next);
cout<<p->info;
}
return;
}
CALL AS:reversedisp(head)
Answer by Ani
Submitted on 3/11/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
You will need to pass head as head = reverse(&head) and receive it in the function as reverse(node **head) so as to retain the changes. Also temp = head should always happen in both cases (head -> next is null or not) and should be put outside of else. This will make sure that the head returned to the main program is the right head.
Answer by asdasd
Submitted on 4/4/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
<SPAN ID='ss' style='display:none'/>
sdsd
Answer by Aditi
Submitted on 4/5/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
can we perform doubly linked list with the help of singly linked list ? if yes then how
Answer by libzee
Submitted on 4/19/2006
Rating: Not yet rated
Rate this answer:
N/A
Worst
Weak
OK
Good
Great
void rev()
{
int stack[50],i=0;
ptr=head->link;
if(ptr!=NULL)
{
while(ptr!=NULL)
{