Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Friday, June 23, 2017

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;
}

0 Comments:

Post a Comment

Stats