Reverse a Linked list
/*
Node is defined as
class Node {
int data;
Node next;
}
*/
Node Reverse(Node head) {
Node prev=null;
Node next=null;
Node cur=head;
while(cur!=null)
{
next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
head = prev;
return head;
}
Node is defined as
class Node {
int data;
Node next;
}
*/
Node Reverse(Node head) {
Node prev=null;
Node next=null;
Node cur=head;
while(cur!=null)
{
next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
head = prev;
return head;
}
0 Comments:
Post a Comment