Following is a simple iterative Method for reversing a singly linked list
public node reverse(Node head){
if(head==null) return;
Node current= head;
Node prev= null;
Node next= null;
while(current!=null){
next= current.next;
current.next= prev;
prev= current;
current= next;
}
head= prev;
return head;
}
No comments:
Post a Comment