Reverse a LinkedList

View previous topic View next topic Go down

Reverse a LinkedList

Post  Admin on 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

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: Reverse a LinkedList

Post  Admin on 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

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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