Bird
Raised Fist0
DSA Javaprogramming~20 mins

Create and Initialize Doubly Linked List in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Doubly Linked List after Insertions
What is the printed state of the doubly linked list after inserting 10, 20, and 30 in order?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void insertEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) temp = temp.next;
        temp.next = newNode;
        newNode.prev = temp;
    }

    void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data);
            if (temp.next != null) System.out.print(" <-> ");
            temp = temp.next;
        }
        System.out.println(" <-> null");
    }
}

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.insertEnd(10);
        dll.insertEnd(20);
        dll.insertEnd(30);
        dll.printList();
    }
}
A10 -> 20 -> 30 -> null
B30 <-> 20 <-> 10 <-> null
C10 <-> 20 <-> 30 <-> null
Dnull
Attempts:
2 left
💡 Hint
Trace the insertEnd method and how nodes link forward and backward.
❓ Predict Output
intermediate
1:30remaining
Value of head after initialization and insertion
After creating an empty doubly linked list and inserting one node with value 5, what is the value of head.data?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void insertFront(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }
}

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.insertFront(5);
        System.out.println(dll.head.data);
    }
}
A5
B0
Cnull
DThrows NullPointerException
Attempts:
2 left
💡 Hint
Check what happens when inserting the first node at the front.
🔧 Debug
advanced
2:00remaining
Identify the bug in doubly linked list insertion at front
What error will this code produce when inserting nodes at the front of the doubly linked list?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void insertFront(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }
}

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.insertFront(1);
    }
}
ANullPointerException
BNo error, works fine
CCompilation error: head.prev cannot be resolved
DInfinite loop at runtime
Attempts:
2 left
💡 Hint
Consider what happens when head is null and you try to access head.prev.
❓ Predict Output
advanced
2:00remaining
Result of removing a node from doubly linked list
What is the printed state of the doubly linked list after removing the node with value 20?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void insertEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node temp = head;
        while (temp.next != null) temp = temp.next;
        temp.next = newNode;
        newNode.prev = temp;
    }

    void remove(int data) {
        Node temp = head;
        while (temp != null && temp.data != data) temp = temp.next;
        if (temp == null) return;
        if (temp.prev != null) temp.prev.next = temp.next;
        else head = temp.next;
        if (temp.next != null) temp.next.prev = temp.prev;
    }

    void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data);
            if (temp.next != null) System.out.print(" <-> ");
            temp = temp.next;
        }
        System.out.println(" <-> null");
    }
}

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.insertEnd(10);
        dll.insertEnd(20);
        dll.insertEnd(30);
        dll.remove(20);
        dll.printList();
    }
}
A10 <-> null
B10 <-> 20 <-> 30 <-> null
C20 <-> 30 <-> null
D10 <-> 30 <-> null
Attempts:
2 left
💡 Hint
Trace the remove method and how links are updated.
🧠 Conceptual
expert
1:30remaining
Why is a doubly linked list preferred over singly linked list for certain operations?
Which of the following is the main advantage of a doubly linked list compared to a singly linked list?
AIt guarantees faster search for any element.
BIt allows traversal in both forward and backward directions efficiently.
CIt uses less memory per node than singly linked list.
DIt requires fewer pointer updates during insertion at the end.
Attempts:
2 left
💡 Hint
Think about how nodes link to neighbors in both list types.

Practice

(1/5)
1. What is the main difference between a singly linked list and a doubly linked list?
easy
A. A doubly linked list does not allow traversal in reverse order.
B. A doubly linked list has nodes with pointers to both previous and next nodes.
C. A doubly linked list stores data in arrays instead of nodes.
D. A doubly linked list only has a pointer to the next node.

Solution

  1. Step 1: Understand node pointers in linked lists

    A singly linked list node has one pointer to the next node only.
  2. Step 2: Identify doubly linked list node pointers

    A doubly linked list node has two pointers: one to the next node and one to the previous node.
  3. Final Answer:

    A doubly linked list has nodes with pointers to both previous and next nodes. -> Option B
  4. Quick Check:

    Doubly linked list = two pointers per node [OK]
Hint: Remember: doubly linked means two directions [OK]
Common Mistakes:
  • Confusing singly and doubly linked list pointers
  • Thinking doubly linked lists use arrays
  • Assuming doubly linked lists cannot go backward
2. Which of the following is the correct way to declare a node class for a doubly linked list in Java?
easy
A. class Node { int data; Node next; Node prev; }
B. class Node { int data; Node next; }
C. class Node { int data; Node prev; }
D. class Node { int data; Node next; Node previous; }

Solution

  1. Step 1: Check node fields for doubly linked list

    A doubly linked list node must have data, next pointer, and previous pointer.
  2. Step 2: Match correct field names

    class Node { int data; Node next; Node prev; } uses 'next' and 'prev' which are standard and consistent.
  3. Final Answer:

    class Node { int data; Node next; Node prev; } -> Option A
  4. Quick Check:

    Node has data, next, and prev [OK]
Hint: Node needs both next and prev pointers [OK]
Common Mistakes:
  • Omitting prev pointer in node class
  • Using inconsistent pointer names
  • Confusing singly linked list node with doubly linked
3. What will be the output of the following Java code that creates a doubly linked list with nodes 10, 20, 30 and prints it forward?
class Node {
  int data;
  Node next;
  Node prev;
  Node(int d) { data = d; next = null; prev = null; }
}

public class Main {
  public static void main(String[] args) {
    Node head = new Node(10);
    Node second = new Node(20);
    Node third = new Node(30);
    head.next = second; second.prev = head;
    second.next = third; third.prev = second;

    Node current = head;
    while(current != null) {
      System.out.print(current.data + " -> ");
      current = current.next;
    }
    System.out.print("null");
  }
}
medium
A. 30 -> 20 -> 10 -> null
B. null -> 10 -> 20 -> 30
C. 10 -> 20 -> 30 -> null
D. 10 -> 20 -> null

Solution

  1. Step 1: Understand list creation and linking

    Nodes 10, 20, 30 are linked forward: 10.next=20, 20.next=30.
  2. Step 2: Trace the print loop

    Starting at head (10), print data then move to next until null: prints 10 -> 20 -> 30 -> null.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option C
  4. Quick Check:

    Forward traversal prints nodes in insertion order [OK]
Hint: Follow next pointers from head to null [OK]
Common Mistakes:
  • Printing in reverse order by mistake
  • Missing last node in output
  • Confusing prev and next pointers
4. Identify the error in the following code snippet that tries to create a doubly linked list with nodes 1 and 2:
class Node {
  int data;
  Node next;
  Node prev;
  Node(int d) { data = d; next = null; prev = null; }
}

public class Main {
  public static void main(String[] args) {
    Node head = new Node(1);
    Node second = new Node(2);
    head.next = second;
    // Missing line here
    System.out.println(head.next.data);
  }
}
medium
A. Missing assignment of second.prev to head
B. Node constructor is missing initialization
C. head.next should be assigned to null
D. Cannot print data without a loop

Solution

  1. Step 1: Check linking between nodes

    head.next is assigned second, but second.prev is not set to head.
  2. Step 2: Identify missing backward link

    For doubly linked list, second.prev must point back to head to maintain two-way links.
  3. Final Answer:

    Missing assignment of second.prev to head -> Option A
  4. Quick Check:

    Both next and prev must be linked [OK]
Hint: Check both next and prev pointers after linking nodes [OK]
Common Mistakes:
  • Forgetting to link prev pointer
  • Assuming next pointer is enough
  • Ignoring bidirectional links in doubly linked list
5. You want to create a doubly linked list from an array of integers [5, 10, 15] in Java. Which approach correctly initializes the list with proper next and prev links?
hard
A. Create nodes in a loop, link each node's next to the previous node, and set previous node's prev to current node.
B. Create nodes in a loop, link each node's prev to the next node, and set next node's next to current node.
C. Create nodes in a loop, link each node's prev to null, and next to null.
D. Create nodes in a loop, link each node's next to the next node, and set next node's prev to current node.

Solution

  1. Step 1: Create nodes from array elements

    For each integer, create a new node with data set to that integer.
  2. Step 2: Link nodes forward and backward

    Set current node's next to next node, and next node's prev to current node to maintain doubly linked structure.
  3. Final Answer:

    Create nodes in a loop, link each node's next to the next node, and set next node's prev to current node. -> Option D
  4. Quick Check:

    Proper doubly linked list needs next and prev links set correctly [OK]
Hint: Link next forward and prev backward in loop [OK]
Common Mistakes:
  • Mixing up next and prev pointers
  • Not setting prev pointer at all
  • Setting prev pointer incorrectly to next node
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard