Bird
Raised Fist0
DSA C#programming~20 mins

Doubly Linked List Structure and Node Design in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Adding Nodes to Doubly Linked List
What is the printed state of the doubly linked list after adding three nodes with values 10, 20, and 30 in that order?
DSA C#
public class Node {
    public int Value;
    public Node Prev;
    public Node Next;
    public Node(int value) {
        Value = value;
        Prev = null;
        Next = null;
    }
}

public class DoublyLinkedList {
    public Node Head;
    public Node Tail;

    public void AddLast(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = Tail = newNode;
        } else {
            Tail.Next = newNode;
            newNode.Prev = Tail;
            Tail = newNode;
        }
    }

    public void PrintList() {
        Node current = Head;
        while (current != null) {
            System.Console.Write(current.Value);
            if (current.Next != null) System.Console.Write(" <-> ");
            current = current.Next;
        }
        System.Console.WriteLine(" <-> null");
    }
}

var list = new DoublyLinkedList();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
list.PrintList();
A30 <-> 20 <-> 10 <-> null
Bnull
C10 <-> 20 <-> null
D10 <-> 20 <-> 30 <-> null
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the list is printed from head to tail.
❓ Predict Output
intermediate
2:00remaining
Output After Removing a Node from Doubly Linked List
What is the printed state of the doubly linked list after removing the node with value 20 from the list 10 <-> 20 <-> 30 <-> null?
DSA C#
public class Node {
    public int Value;
    public Node Prev;
    public Node Next;
    public Node(int value) {
        Value = value;
        Prev = null;
        Next = null;
    }
}

public class DoublyLinkedList {
    public Node Head;
    public Node Tail;

    public void AddLast(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = Tail = newNode;
        } else {
            Tail.Next = newNode;
            newNode.Prev = Tail;
            Tail = newNode;
        }
    }

    public void Remove(int value) {
        Node current = Head;
        while (current != null) {
            if (current.Value == value) {
                if (current.Prev != null) current.Prev.Next = current.Next;
                else Head = current.Next;
                if (current.Next != null) current.Next.Prev = current.Prev;
                else Tail = current.Prev;
                break;
            }
            current = current.Next;
        }
    }

    public void PrintList() {
        Node current = Head;
        while (current != null) {
            System.Console.Write(current.Value);
            if (current.Next != null) System.Console.Write(" <-> ");
            current = current.Next;
        }
        System.Console.WriteLine(" <-> null");
    }
}

var list = new DoublyLinkedList();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
list.Remove(20);
list.PrintList();
A10 <-> 30 <-> null
B10 <-> 20 <-> 30 <-> null
C20 <-> 30 <-> null
D10 <-> null
Attempts:
2 left
💡 Hint
Removing a node updates the previous and next nodes to skip the removed node.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Node Insertion at Head
What error will occur when running this code that inserts a node at the head of a doubly linked list?
DSA C#
public class Node {
    public int Value;
    public Node Prev;
    public Node Next;
    public Node(int value) {
        Value = value;
        Prev = null;
        Next = null;
    }
}

public class DoublyLinkedList {
    public Node Head;
    public Node Tail;

    public void AddFirst(int value) {
        Node newNode = new Node(value);
        newNode.Next = Head;
        if (Head != null) Head.Prev = newNode;
        Head = newNode;
        if (Tail == null) Tail = newNode;
    }
}

var list = new DoublyLinkedList();
list.AddFirst(10);
ANo error, list has one node with value 10
BNullReferenceException because Head is null when accessing Head.Prev
CStackOverflowException due to infinite recursion
DCompile-time error due to missing semicolon
Attempts:
2 left
💡 Hint
Check what happens when the list is empty and you access Head.Prev.
🧠 Conceptual
advanced
1:30remaining
Understanding Node Links in Doubly Linked List
In a doubly linked list, what is the correct relationship between a node and its neighbors?
ANode's Next and Prev both point to the next node
BNode's Next points to the previous node; Node's Prev points to the next node
CNode's Next points to the next node; Node's Prev points to the previous node
DNode's Next and Prev both point to the previous node
Attempts:
2 left
💡 Hint
Think about moving forward and backward in the list.
🚀 Application
expert
3:00remaining
Result of Reversing a Doubly Linked List
After reversing a doubly linked list with nodes 1 <-> 2 <-> 3 <-> null, what is the printed state of the list?
DSA C#
public class Node {
    public int Value;
    public Node Prev;
    public Node Next;
    public Node(int value) {
        Value = value;
        Prev = null;
        Next = null;
    }
}

public class DoublyLinkedList {
    public Node Head;
    public Node Tail;

    public void AddLast(int value) {
        Node newNode = new Node(value);
        if (Head == null) {
            Head = Tail = newNode;
        } else {
            Tail.Next = newNode;
            newNode.Prev = Tail;
            Tail = newNode;
        }
    }

    public void Reverse() {
        Node current = Head;
        Node temp = null;
        while (current != null) {
            temp = current.Prev;
            current.Prev = current.Next;
            current.Next = temp;
            current = current.Prev;
        }
        if (temp != null) {
            Head = temp.Prev;
        }
    }

    public void PrintList() {
        Node current = Head;
        while (current != null) {
            System.Console.Write(current.Value);
            if (current.Next != null) System.Console.Write(" <-> ");
            current = current.Next;
        }
        System.Console.WriteLine(" <-> null");
    }
}

var list = new DoublyLinkedList();
list.AddLast(1);
list.AddLast(2);
list.AddLast(3);
list.Reverse();
list.PrintList();
A3 <-> 2 <-> 1 <-> null
B1 <-> 2 <-> 3 <-> null
Cnull
D1 <-> 3 <-> 2 <-> null
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates the head.

Practice

(1/5)
1. What is the main advantage of a Doubly Linked List compared to a singly linked list?
easy
A. It allows traversal in both forward and backward directions.
B. It uses less memory than a singly linked list.
C. It does not require a head pointer.
D. It automatically sorts the elements.

Solution

  1. Step 1: Understand the structure difference

    A doubly linked list has nodes with two pointers: one to the next node and one to the previous node.
  2. Step 2: Identify traversal capabilities

    This allows moving forward and backward through the list, unlike singly linked lists which only move forward.
  3. Final Answer:

    It allows traversal in both forward and backward directions. -> Option A
  4. Quick Check:

    Doubly linked list = two-way traversal [OK]
Hint: Remember: two pointers mean two directions [OK]
Common Mistakes:
  • Thinking it uses less memory than singly linked list
  • Assuming it doesn't need a head pointer
  • Believing it sorts elements automatically
2. Which of the following is the correct way to declare a node class for a doubly linked list in C#?
easy
A. class Node { public int data; public Node next; }
B. public class Node { public int data; public Node next; public Node prev; }
C. public class Node { int data; Node next; }
D. public struct Node { int data; Node next; Node prev; }

Solution

  1. Step 1: Check for public access and all pointers

    The node must have public fields for data, next, and prev to represent doubly linked list nodes.
  2. Step 2: Verify syntax correctness

    public class Node { public int data; public Node next; public Node prev; } declares all required fields with public access, suitable for a doubly linked list node.
  3. Final Answer:

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

    Node class with data, next, prev = public class Node { public int data; public Node next; public Node prev; } [OK]
Hint: Doubly linked node needs next and prev pointers public [OK]
Common Mistakes:
  • Omitting the prev pointer
  • Using struct instead of class (less common for linked lists)
  • Not making fields public for access
3. What will be the output of the following C# code snippet for a doubly linked list node?
Node node = new Node();
node.data = 10;
node.next = null;
node.prev = null;
Console.WriteLine(node.data);
medium
A. 10
B. null
C. 0
D. Compilation error

Solution

  1. Step 1: Analyze node initialization

    The node's data is set to 10 explicitly, and next and prev are set to null.
  2. Step 2: Check the printed value

    The Console.WriteLine prints node.data, which is 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    node.data = 10 prints 10 [OK]
Hint: Prints the assigned data value, not pointers [OK]
Common Mistakes:
  • Confusing null pointers with data value
  • Expecting default 0 instead of assigned 10
  • Thinking it causes compilation error
4. Identify the error in this doubly linked list node insertion code snippet:
Node newNode = new Node();
newNode.data = 5;
newNode.next = head;
head.prev = newNode;
head = newNode;
medium
A. Missing initialization of newNode.prev to null
B. head.prev should be assigned after head is updated
C. No error, code correctly inserts at the beginning
D. newNode.next should be null

Solution

  1. Step 1: Understand insertion at list start

    newNode's next points to current head, head's prev points back to newNode, then head updates to newNode.
  2. Step 2: Check pointer assignments

    newNode.prev is null by default or not needed explicitly here; the code correctly links nodes.
  3. Final Answer:

    No error, code correctly inserts at the beginning -> Option C
  4. Quick Check:

    Insertion at head with correct pointers = No error, code correctly inserts at the beginning [OK]
Hint: Check pointer updates order carefully [OK]
Common Mistakes:
  • Assuming prev must be set explicitly to null
  • Thinking head.prev must be set after head update
  • Confusing next pointer assignment
5. You want to design a method to remove a node from the middle of a doubly linked list. Which pointers must be updated to maintain list integrity?
hard
A. Only the head pointer
B. The tail pointer and the node's prev pointer
C. Only the node's next pointer
D. The previous node's next and the next node's prev pointers

Solution

  1. Step 1: Identify nodes around the removed node

    To remove a node, you must link its previous node to its next node directly.
  2. Step 2: Update pointers to bypass the removed node

    Set previous node's next to removed node's next, and next node's prev to removed node's prev.
  3. Final Answer:

    The previous node's next and the next node's prev pointers -> Option D
  4. Quick Check:

    Update prev.next and next.prev to remove node [OK]
Hint: Update both prev and next pointers around removed node [OK]
Common Mistakes:
  • Only updating one pointer and breaking the list
  • Trying to update head or tail unnecessarily
  • Ignoring the prev pointer of the 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