program to Reverse a linked list

Answers were Sorted based on User's Feedback



program to Reverse a linked list..

Answer / raghuram

node *reverse(node *first)


{
node *cur,*temp;


cur=NULL;

while(first!=NULL)


{temp=first;

first=first->link;

temp->link=cur;

cur=temp;
}

return cur;

}

Is This Answer Correct ?    137 Yes 52 No

program to Reverse a linked list..

Answer / shruti

reverse a linked list -> by creating a new list.

consider home as the stationary pointer of the original list
adn start as the stationary pointer of the new linked list..

the node structure is as follows:

struct node
{
int data;
struct node * next;
}

struct node * reverse(struct node *home , struct node *
start)
temp = home;
while(temp != NULL)
{
p = (struct node *) malloc (sizeof(struct node));
p -> data = temp -> data;
p -> next = NULL;

if(start == NULL)
start = p;

else
{
p -> next = start;
start = p;
}

temp = temp -> next;

return start;
}

Is This Answer Correct ?    42 Yes 16 No

program to Reverse a linked list..

Answer / gaurav jain

void reverse(struct node **kbr)
{
struct node *temp,*p,*q;
temp=*kbr;
p=temp->next;
q=temp;
while(p!=NULL)
{
temp=p;
p=p->next;
temp->next=q;
q=temp;
}
(*kbr)->next=NULL;
*kbr=q;
}

Is This Answer Correct ?    24 Yes 16 No

program to Reverse a linked list..

Answer / ashish rajta

void reverse(list *l)
{
lnode *temp;
temp=l->first;
while(temp->next != NULL)
{
temp=temp->next;
}
while(temp !=NULL)
{
printf("%d",temp->info);
temp=temp->prev;
}
}

Is This Answer Correct ?    15 Yes 12 No

program to Reverse a linked list..

Answer / ajay george

void rev(node *nPtr, node *prevPtr)
{
if(nPtr->next==NULL)
{
head=nPtr;
}
else
{
rev(nPtr->next, nPtr);
}
nPtr->next=prevPtr;
prevPtr->next=NULL;
}

Is This Answer Correct ?    3 Yes 1 No

program to Reverse a linked list..

Answer / mbm

/* Reverse linked list by recursion */

if(head)
head = _ReverseLinkedList(NULL, head, head->next);

NodeStr *_ReverseLinkedList(
NodeStr *preNode,
NodeStr *node1,
NodeStr *node2
)
{
NodeStr *next_node;

if(!node2)
return node1;

next_node = node2->next;
node2->next = node1;
node1->next = preNode;
return _ReverseLinkedList(node1, node2, next_node);
}

Is This Answer Correct ?    11 Yes 11 No

program to Reverse a linked list..

Answer / raj

void linkreverse()
{
node *p=head,*q=head->next,*r=q->next,*temp;
p->next=NULL;
temp=p;
while(p!=tail)
{
q->next=p;
p=q;
if(r!=NULL)
{
q=r;
r=q->next;
}
}
head=p;
tail=temp;
}

Is This Answer Correct ?    0 Yes 0 No

program to Reverse a linked list..

Answer / cleonjoys

void reverse(Node_t *nPtr, Node_t *prevPtr){

if(nPtr->next != NULL){
reverse(nPtr->next, nPtr);
}
else{
headp = nPtr; //header is a global pointer, i know
keeping header is security hole.
}
nPtr->next = prevPtr;
}

Is This Answer Correct ?    7 Yes 8 No

program to Reverse a linked list..

Answer / bragaadeesh

Program to reverse a singly list ITERATIVELY,

http://www.technicalypto.com/2010/01/java-program-to-reverse-singly-linked.html

Program to reverse a linked list RECURSIVELY

http://www.technicalypto.com/2010/03/reverse-singly-linked-list-recursively.html

Is This Answer Correct ?    4 Yes 7 No

program to Reverse a linked list..

Answer / don

int rec_rev(node *p) {
node *last_prev,*last,*temp,*q;
if (p->next==NULL)
return 1;
else {
last_prev=find_last(); // finds last but 1 element
last=last_prev->next;
last_prev->next==NULL;
if(p==head) {
temp=head;
head=last;
head->next=temp;
p=temp;
rec_rev(p);
}
else {
q=find_prev(p);
last->next=q->next;
q->next=last;
q->next->next=p;
rec_rev(p);
}
}
]

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More C Code Interview Questions

main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


Write a complete program that consists of a function that can receive two numbers from a user (M and N) as a parameter. Then print all the numbers between the two numbers including the number itself. If the value of M is smaller than N, print the numbers in ascending flow. If the value of M is bigger than N, print the numbers in descending flow. may i know how the coding look like?

2 Answers  


write a program to count the number the same (letter/character foreg: 's') in a given sentence.

2 Answers  


3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.

0 Answers   RoboSoft,


#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }

1 Answers  






#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

1 Answers  


#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

1 Answers  


#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }

3 Answers  


how to swap 3 nos without using temporary variable

4 Answers   Satyam,


main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }

2 Answers  


plz tell me the solution.......... in c language program guess any one number from 1 to 50 and tell that number within 8 asking question in yes or no...............

2 Answers   Wipro,


How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,


Categories