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

abcdedcba abc cba ab ba a a

2 Answers  


Write a C program to print look and say sequence? For example if u get the input as 1 then the sequence is 11 21 1211 111221 312211 12112221 .......(it counts the no. of 1s,2s etc which is in successive order) and this sequence is used in run-length encoding.

1 Answers  


main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }

1 Answers  


How do you create a really large matrix (i.e. 3500x3500) in C without having the program crash? I can only reach up to 2500. It must have something to do with lack of memory. Please help!

1 Answers  


What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }

1 Answers  






main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

1 Answers  


void ( * abc( int, void ( *def) () ) ) ();

1 Answers  


void main () { int x = 10; printf ("x = %d, y = %d", x,--x++); } a. 10, 10 b. 10, 9 c. 10, 11 d. none of the above

2 Answers   HCL,


In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }

1 Answers  


Is the following code legal? struct a { int x; struct a b; }

1 Answers  


int main() { int x=10; printf("x=%d, count of earlier print=%d", x,printf("x=%d, y=%d",x,--x)); getch(); } ================================================== returns error>> ld returned 1 exit status =================================================== Does it have something to do with printf() inside another printf().

1 Answers  


main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

5 Answers   HCL,


Categories