Languages
[Edit]
EN

Java - insert node as first one to double linked list (custom implementation)

6 points
Created by:
Indira
499

1. Overview

Edit

In this post we will see how to implement algorithm to correctly add node as first one to double linked list. Below we have 2 implementations, first one with node based on Integer, second one with generic Node and usage example based on String. To create new Node we call constructor with previous node set as null and next node set as newly created one.Then we have short logic which will handle linking our nodes together. We can use the logic to reverse the process of adding the node as first one and implement logic for appending the node as the last one. In next article we cover the algorithm of inserting node to the end of linked list (adding node as last one).

2. Insert node - code example

Edit

Below we have java implementation with Node based on Integer as item of double linked list with logic to insert node as first one. This implementation is without generics to have this example as simple as possible. Generic implementation of this linked list is in next part of this post.

Output:

Java - insert node as first one to double linked list - screenshot from intellij idea in debugging session
Debug session - Insert node to linked list

3. Insert node - generic implementation

Edit

Below we have java generic implementation of double linked list with add first method which will add node to our list.

Output:

Java - insert node as first one to double linked list - screenshot from intellij idea in debugging session - generic implementation
Debug session - Insert node to linked list - generic implementation

References

Edit

References (Harvard CS classes - Data Structures)

Edit

Alternative titles

  1. Add node at the beginning of linked list in java
  2. Java - append entry node as first one to the linked list
  3. How do I add node to the double linked list at the beginning in java?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join