43

I see JDK implementation of LinkedList internally contains Node inner class, which contains the address to next and previous.

So my doubt isn't LinkedList in java a doubly linked list. If not, why?

And how to implement our own doubly linked list?

3
  • This link discusses doubly-linked List and Deque implementations. Is that what you're linking for? Commented Jul 12, 2015 at 8:51
  • 1
    Ironically, there is no singly-linked list implementation in JDK: Why LinkedList in Java is not a real Linked List? Commented May 23, 2019 at 16:59
  • Is there a built in circular linked list implementation in java too.. ? Commented Aug 26, 2019 at 16:21

3 Answers 3

76

Yes, LinkedList is a doubly linked list, as the Javadoc mentions :

Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null).

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

Sign up to request clarification or add additional context in comments.

14 Comments

linkedList.listIterator().previous().
@pvg ListIterator<E> listIterator(int index); does exactly that. Of course it has to traverse the linked list (either from the start or from the end) to get to the required node.
@AvinashJethy ListIterator allows iteration in both directions. listIterator(i).previous() is what you are looking for.
@pvg A node interface gives you constant time access to prev/next - you still have to traverse a linked list (from the start or from the end) until you reach that node. That's exactly what listIterator(i) does (and get(i) does as much work). After you obtain the ListIterator, previous and next run in constant time.
@AvinashJethy list.listIterator(i) gives you access to the elements before and after the i'th element as well as their indices. It's not clear what else you need that's not covered by that. Even if you had access to the internal Node/Entry class, you'd have to iterate from the first or last Node of the list to reach the i'th Node. That's what ListIterator does for you.
|
1

What's missing in java LinkedList is ability to store pointers. Consider the following code:

var list = new LinkedList<Integer>();
var head = list.listIterator();
var anotherHead = list.listIterator();
anotherHead.add(5);
System.out.println(head.next());

We would like to store a pointer to the head and use it later. No way. As soon as the list gets modified (through other pointer for example) our pointer becomes invalid. In a normal linked list the old pointer should still allow us to navigate through the list.

1 Comment

This one of the issues with Java's linked list class compared to C++ std::list:.
-1

This Java code implements a double linked list using the built-in LinkedList class from the Java Collections Framework. The DoubleLinkedList class has methods to add elements, remove elements, and print the list in both forward and backward directions.

import java.util.LinkedList;
import java.util.ListIterator;

class DoubleLinkedList {

    LinkedList linklist = new LinkedList();

    public void add(int item) {
        linklist.add(item);

    }

    public int remove() {
        return (int) linklist.remove();
    }

    public void printlist() {
        ListIterator<Integer> lstr = linklist.listIterator();
        System.out.println("printing data forward direction");
        while (lstr.hasNext()) {
            System.out.println(lstr.next());
        }
        System.out.println("printing data backward direction");
        while (lstr.hasPrevious()) {
            System.out.println(lstr.previous());
        }
    }

}

public class DoubleLinkedListUsingBuiltInLinkedList {

    public static void main(String[] args) {
        DoubleLinkedList d1 = new DoubleLinkedList();
        d1.add(10);
        d1.add(20);
        d1.add(30);
        
        d1.printlist();
        
    }
    
}

Note that this implementation uses the built-in LinkedList class, which is a doubly-linked list. This means that each node in the list has references to both the previous and next nodes, allowing for efficient insertion and removal of elements at any position in the list.

Also, the printlist method uses a ListIterator to print the elements in both forward and backward directions. The ListIterator is a specialized iterator that allows for bidirectional traversal of the list.

Overall, this code demonstrates how to use the built-in LinkedList class to implement a double linked list and perform basic operations like adding, removing, and printing elements.

2 Comments

You posted the exact, same code in your other answer.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.