How to insert a node at the beginning of a Singly Linked List in Java?

By | June 17, 2020

In the previous post, Representation of Singly Linked List in Java,we learned How Custom Linked List represented internally.

In this post, We will learn How to insert a node at the beginning 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:

  1. It has a sequence of nodes.
  2. Every Node has two parts, the first part is data, and the second part is the reference to the next node in the List.
  3. First Node is called head Node.
  4. Last node always point to NULL

Singly LinkedList

Inserting a Node at the beginning of a Singly Linked List:

Case 1: If Linked List is empty Then adding newNode at the beginning:

 

Case 2:If Linked List is not empty Then adding newNode at the beginning:

Output of this Program: 

20->10->null

You May Also Like:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *