public void addAtStart(int data) {
            Node newNode = new Node(data);  
      
            if(head == null) {  
              head = newNode;  
              tail = newNode;  
            }  
            else {  
              Node temp = head;  
              head = newNode;  
              head.next = temp;  
            }  
        }  

      Java language logo
      Add Node at start of linked list

      Axt

      The addAtStart() method creates a new Node object and sets the data field to the given value. If the head node is null, the addAtStart() method creates a new Node object and sets the data field to the given value. The newNode object's next field references the head node. If the head node is not null, the addAtStart() method sets the head node's next field to the newNode object.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.