Inserting a Node at the Tail of a Linked List

/*
  Insert Node at the end of a linked list
  head pointer input could be NULL as well for empty list
  Node is defined as


  class Node {
     int data;
     Node next;
  }


*/




Node Insert(Node head,int data) {
    Node n = new Node();
    n.data = data;
    n.next = null;
    
    if(head == null){
        head = n;
        return head;
    }
    
    else
    {
        Node c = head;
        while(c.next != null){
            c = c.next;
        }
        
        c.next = n;
        
        return head;
        
    }
}

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations