In the previous post, How to insert a node at the beginning of a Singly Linked List in Java, we learned, How to Insert a Node at the beginning of the Singly LinkedList.
In this post, We will learn How to Insert a node at the end of a Singly Linked List in Java?
Singly LinkedList is a Data Structure used to store the collection of nodes and having the following properties:
- It has a sequence of nodes.
- Every Node has two parts, the first part is data, and the second part is the reference to the next node in the List.
- First Node is called head Node.
- Last node always point to NULL
Inserting a Node at the End of a Singly Linked List:
Inserting a new Node at the End of a Singly Linked List:
CASE 1: If Linked List is empty Then adding newNode at End:
CASE 2: If Linked List is not empty, Then adding a newNode at End:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.kkjavatutorials.util; /** * How to insert a node at the beginning and End of a Singly Linked List in Java? * @author KK JavaTutorials */ public class LinkedList<T> { private Node<T> head; //This internal class represents Node in Linked List private static class Node<T>{ private T data; private Node<T> next; public Node(T data) { super(); this.data = data; this.next = null; } } /** * This method insert a Node at the beginning of the List * @param data has to insert in the List */ public void insertFirst(T data) { Node<T> newNode = new Node<T>(data); newNode.next = head; head = newNode; } /** * This method insert a Node at the End of the List * @param data has to insert in the List */ public void insertLast(T data) { Node<T> newNode = new Node<T>(data); if(head == null) { head = newNode; }else { Node<T> currentNode = head; while (currentNode.next != null) { currentNode = currentNode.next; } currentNode.next = newNode; } } /** * Method which traverse Linked List and display all data. */ public void displayLinkedList() { Node<T> currentNode = head; while(currentNode!= null) { System.out.print(currentNode.data+"->"); currentNode = currentNode.next; } System.out.println(currentNode); } public static void main(String[] args) { LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.insertFirst(10); linkedList.insertFirst(20); linkedList.displayLinkedList(); System.out.println("Adding two newNodes 30 and 40 at last..."); linkedList.insertLast(30); linkedList.insertLast(40); linkedList.displayLinkedList(); } } |
Output of this Program:
20->10->null
Adding two newNodes 30 and 40 at last…
20->10->30->40->null
You May Also Like:
- Queue Implementation using LinkedList in java
- Queue Implementation using an array in java
- Queue Data Structure
- How to Implement Stack in java using Linked List ?
- Stack implementation in Java using array
- Java Program to find the frequency of each character in String ?
That’s all about the How to Insert node at the end of a Singly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.