Code For NonGeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Reverse a LinkedList

Go down

Reverse a LinkedList Empty Reverse a LinkedList

Post  Admin Fri Aug 26, 2011 12:31 pm

public ListNode reverse(ListNode head){
if(head==null)
return null;
if(head.next==null)
return head;
ListNode current=head;
ListNode prev=null;
ListNode temp=null;
while(current!=null){
temp=current.next;
current.next=prev;
prev=current;
current=temp;
}
return prev;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

https://codefornongeek.forumotion.com

Back to top Go down

Reverse a LinkedList Empty Re: Reverse a LinkedList

Post  Admin Tue Sep 27, 2011 1:18 am

Recursive way:

Recursive Method:

reverseList(List, NULL)

Node * reverseList(Node *node, Node *remainder)
{
Node *tmpNode;
if (node==NULL) return remainder;
tmpNode= node->next;
node->next = remainder;
reverseList(tmpNode, node);

}

Admin
Admin

Posts : 131
Join date : 2011-08-16

https://codefornongeek.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum