Insertion in Beginning in a Linked List in Java

Insertion in Beginning in JAVA

Collection of Data Elements along with the address to their consecutive element is called a Linked List. For Insertion in Beginning in A Linked List in JAVA We’ll first have to store the address to the previous first element along with the new element that will be inserted into the list. And also the head will now point towards the address of the new element.

Insertion In Beginning of a Linked List

Implementation

  • To insert a node in the beggining of a linked list, we first have to check the Head’s Reference to the first node of a linked list.
  •  If the head is equal to null, then the list is already empty else the list already has an element whose reference is stored by the head. 
  • To insert an element, in the beginning, we will have to replace the address stored by the head with the address of the new element we wish to insert. 
  • The address space of the previously stored element will now be stored in the pointer reference of the inserted element.
Linked List Insertion at Beginning in Java

Algorithm for Insertion in Beginning in A Linked List in JAVA

  • IF HEAD == NULL
    EXIT
  • ELSE
  • NEW_NODE = ADDNODE()
  • NEW_NODE -> DATA = ITEM
  • PTR = HEAD
  • NEW -> NEXT = PTR
  • HEAD = NEW_NODE
  • EXIT
Singly Linked List Insertion at the beginning in Java

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription