Skip to main content

Adding Node at the End of Linked List

Adding Node at the End of Linked List:-

The method takes two parameter one a head node of a linked list and other data too insert.


 public void addNodeEnd(Node head,int data)
 {
  if(head==null)
  {
   System.out.println("list is empty");
   return;
  }
 Node node=new Node(data);
 Node temp=head;
 while(temp.next!=null)
 {
  temp=temp.next;
 }
 temp.next=node;
 }

To Know the basic structure of the linked list click here


Comments

.

Popular posts from this blog

Tree Traversal in JAVA (InOder/preOrder/postOrder)

Tree Traversal in JAVA (InOder/preOrder/postOrder):-

Tree traversal can be done through three ways.

1)Inorder:-

Go recursively to the left node.
Read a node
Go recursively to the right node.

2)Pre Order.

Read a node
Go recursively to the left node.
Go recursively to the right node.


2)Post Order.


Go recursively to the left node.
Go recursively to the right node.
Read a node



publicvoidinorder(Node root){if(root==null){return;} inorder(root.left); System.out.print(root.data+","); inorder(root.right);}publicvoidpreOrder(Node root){if(root==null){return;} System.out.print(root.data+","); preOrder(root.left); preOrder(root.right);}publicvoidpostOrder(Node root){if(root==null){return;} postOrder(root.left); postOrder(root.right); System.out.print(root.data+",");}