edited by
1,079 views
0 votes
0 votes

To reverse a Singly Linked List is the below is correct code? (or) need to change 

Struct node *reverse(struct node *start)
{
Struct node *prev,*ptr,*next;
prev=NULL;
ptr=start;
while(ptr!=NULL)
{
next=ptr->link;
ptr->link=prev;
prev=ptr;
ptr=next;
}
start=prev;
return start;

 

 

Plz tell me, is here all link updating correctly?

edited by

1 Answer

0 votes
0 votes
reverse(p=q=null)

{

while(s!=0)

{p=q;

q=s;

s=s->next;

q->next=p;

}

s=q;

return(s);

}

 

try this program to reverse tha single link list.

 

where p and q having extra 2 poniter.

Related questions

1 votes
1 votes
1 answer
1
2 votes
2 votes
1 answer
2
rahul sharma 5 asked Sep 28, 2017
7,761 views
if we implement queue using singly linked list then how juch time enqueue enqueue and dequeue will take ?
0 votes
0 votes
1 answer
3
Mrityudoot asked Feb 25
144 views
How can we find the highest element in a singly linked list in O(1)? We are free to use any extra space.