Bird
Raised Fist0
DSA C#programming~20 mins

Create and Initialize Doubly Linked List in DSA C# - 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 nodes with values 10, 20, and 30 in that order at the end?
DSA C#
public class Node {
    public int Data;
    public Node Prev;
    public Node Next;
    public Node(int data) {
        Data = data;
        Prev = null;
        Next = null;
    }
}

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

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        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.Data);
            if (current.Next != null) System.Console.Write(" <-> ");
            current = current.Next;
        }
        System.Console.WriteLine(" <-> null");
    }
}

var list = new DoublyLinkedList();
list.InsertAtEnd(10);
list.InsertAtEnd(20);
list.InsertAtEnd(30);
list.PrintList();
A30 <-> 20 <-> 10 <-> null
B10 <-> 20 <-> 30 <-> null
C10 -> 20 -> 30 -> null
Dnull <-> 10 <-> 20 <-> 30
Attempts:
2 left
💡 Hint
Remember that InsertAtEnd adds nodes at the tail, so the order is the same as insertion.
❓ Predict Output
intermediate
2:00remaining
State of Doubly Linked List after Deleting Head
Given a doubly linked list with nodes 5 <-> 15 <-> 25 <-> null, what is the printed state after deleting the head node?
DSA C#
public class Node {
    public int Data;
    public Node Prev;
    public Node Next;
    public Node(int data) {
        Data = data;
        Prev = null;
        Next = null;
    }
}

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

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

    public void DeleteHead() {
        if (Head == null) return;
        if (Head == Tail) {
            Head = Tail = null;
        } else {
            Head = Head.Next;
            Head.Prev = null;
        }
    }

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

var list = new DoublyLinkedList();
list.InsertAtEnd(5);
list.InsertAtEnd(15);
list.InsertAtEnd(25);
list.DeleteHead();
list.PrintList();
A15 <-> 25 <-> null
B5 <-> 15 <-> 25 <-> null
C25 <-> 15 <-> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head removes the first node and updates the head pointer.
🔧 Debug
advanced
2:00remaining
Identify the Bug in InsertAtBeginning Method
What is the bug in the InsertAtBeginning method of this doubly linked list implementation?
DSA C#
public class Node {
    public int Data;
    public Node Prev;
    public Node Next;
    public Node(int data) {
        Data = data;
        Prev = null;
        Next = null;
    }
}

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

    public void InsertAtBeginning(int data) {
        Node newNode = new Node(data);
        if (Head == null) {
            Head = newNode;
            Tail = newNode;
        } else {
            newNode.Next = Head;
            Head.Prev = newNode;
            // Missing update of Head pointer
        }
    }
}
ATail pointer is incorrectly set to newNode in non-empty list.
BNext pointer of Head is incorrectly set to newNode.
CPrev pointer of newNode is incorrectly set to Head.
DHead pointer is not updated to newNode after insertion.
Attempts:
2 left
💡 Hint
Check if the head pointer points to the new first node after insertion.
❓ Predict Output
advanced
2:00remaining
Output after Reversing a Doubly Linked List
What is the printed state of the doubly linked list after reversing it? Initial list: 1 <-> 2 <-> 3 <-> null
DSA C#
public class Node {
    public int Data;
    public Node Prev;
    public Node Next;
    public Node(int data) {
        Data = data;
        Prev = null;
        Next = null;
    }
}

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

    public void InsertAtEnd(int data) {
        Node newNode = new Node(data);
        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.Data);
            if (current.Next != null) System.Console.Write(" <-> ");
            current = current.Next;
        }
        System.Console.WriteLine(" <-> null");
    }
}

var list = new DoublyLinkedList();
list.InsertAtEnd(1);
list.InsertAtEnd(2);
list.InsertAtEnd(3);
list.Reverse();
list.PrintList();
A1 <-> 3 <-> 2 <-> null
B1 <-> 2 <-> 3 <-> null
C3 <-> 2 <-> 1 <-> null
Dnull
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head to last node.
🧠 Conceptual
expert
2:00remaining
Memory Usage in Doubly Linked List vs Singly Linked List
Which statement correctly explains the difference in memory usage between a doubly linked list and a singly linked list?
AA doubly linked list uses more memory per node because it stores two pointers, Prev and Next, while a singly linked list stores only one pointer, Next.
BA doubly linked list uses less memory because it can be traversed backwards, reducing the need for extra nodes.
CBoth lists use the same memory per node because pointers are optimized by the compiler.
DA singly linked list uses more memory because it requires additional pointers to track the previous node.
Attempts:
2 left
💡 Hint
Consider how many pointers each node stores in both lists.

Practice

(1/5)
1. What is the main purpose of a doubly linked list in data structures?
easy
A. To allow traversal in both forward and backward directions
B. To store data in a fixed-size array
C. To sort data automatically
D. To store data only at the beginning

Solution

  1. Step 1: Understand doubly linked list structure

    A doubly linked list has nodes with pointers to both next and previous nodes.
  2. Step 2: Identify traversal capability

    This structure allows moving forward and backward through the list easily.
  3. Final Answer:

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

    Doubly linked list = bidirectional traversal [OK]
Hint: Remember: doubly means two directions [OK]
Common Mistakes:
  • Confusing doubly linked list with arrays
  • Thinking it sorts data automatically
  • Assuming it only allows forward traversal
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 { int data; int next; int prev; }
C. public class Node { public int data; public Node next; public Node prev; }
D. public class Node { public int data; public Node next; private Node previous; }

Solution

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

    Node must have data, next pointer, and previous pointer of type Node.
  2. Step 2: Verify correct syntax and access modifiers

    public class Node { public int data; public Node next; public Node prev; } declares public int data, and public Node next and prev correctly.
  3. Final Answer:

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

    Node class with data, next, prev = public class Node { public int data; public Node next; public Node prev; } [OK]
Hint: Node needs next and prev as Node types [OK]
Common Mistakes:
  • Using int for next and prev instead of Node
  • Missing prev pointer for doubly linked list
  • Incorrect access modifiers
3. What will be the output of the following C# code that creates a doubly linked list with nodes 10, 20, 30 and prints forward traversal?
public class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

public class DoublyLinkedList {
    public Node head;
    public void AddLast(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;
    }
    public void PrintForward() {
        Node temp = head;
        while (temp != null) {
            Console.Write(temp.data + " -> ");
            temp = temp.next;
        }
        Console.WriteLine("null");
    }
}

class Program {
    static void Main() {
        DoublyLinkedList list = new DoublyLinkedList();
        list.AddLast(10);
        list.AddLast(20);
        list.AddLast(30);
        list.PrintForward();
    }
}
medium
A. 30 -> 20 -> 10 -> null
B. null
C. 10 -> 20 -> null
D. 10 -> 20 -> 30 -> null

Solution

  1. Step 1: Understand AddLast method behavior

    AddLast adds nodes at the end, so nodes are 10, then 20, then 30 in order.
  2. Step 2: Trace PrintForward output

    PrintForward prints from head to last node: 10 -> 20 -> 30 -> null.
  3. Final Answer:

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

    Forward print = 10 -> 20 -> 30 -> null [OK]
Hint: AddLast appends; PrintForward prints from head [OK]
Common Mistakes:
  • Confusing forward and backward traversal
  • Missing last node in output
  • Printing in reverse order
4. Identify the error in the following code snippet that tries to add a node at the end of a doubly linked list:
public void AddLast(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;
    // Missing line here
}
medium
A. Missing setting newNode.prev = temp
B. Missing setting head = newNode
C. Missing while loop to find last node
D. Missing creating newNode instance

Solution

  1. Step 1: Review AddLast logic

    After finding last node, newNode is linked as next of temp.
  2. Step 2: Check doubly linked list pointer updates

    For doubly linked list, newNode.prev must point back to temp to maintain backward link.
  3. Final Answer:

    Missing setting newNode.prev = temp -> Option A
  4. Quick Check:

    Set newNode.prev to temp for backward link [OK]
Hint: Always set newNode.prev when adding at end [OK]
Common Mistakes:
  • Forgetting to set newNode.prev
  • Reassigning head incorrectly
  • Skipping loop to find last node
5. You want to create a doubly linked list from an array of integers {1, 2, 3, 4} such that the list is 1 -> 2 -> 3 -> 4 (head points to 1), and then reverse print the list (4 -> 3 -> 2 -> 1 -> null) using backward traversal. Which approach correctly initializes the list and prints it backward?
int[] arr = {1, 2, 3, 4};
DoublyLinkedList list = new DoublyLinkedList();
// Step 1: Add all elements from arr to list
// Step 2: Print list backward

// Which code snippet correctly does this?
hard
A. for (int i = arr.Length - 1; i >= 0; i--) list.AddLast(arr[i]); Node temp = list.head; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.next; } Console.WriteLine("null");
B. foreach (int val in arr) list.AddLast(val); Node temp = list.head; while (temp.next != null) temp = temp.next; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.prev; } Console.WriteLine("null");
C. foreach (int val in arr) list.AddFirst(val); Node temp = list.head; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.next; } Console.WriteLine("null");
D. for (int i = 0; i < arr.Length; i++) list.AddLast(arr[i]); Node temp = list.head; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.next; } Console.WriteLine("null");

Solution

  1. Step 1: Add elements in order using AddLast

    Use foreach (int val in arr) list.AddLast(val); to build the list as 1 -> 2 -> 3 -> 4, preserving array order.
  2. Step 2: Traverse to last node and print backward

    Node temp = list.head; while (temp.next != null) temp = temp.next; then while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.prev; } Console.WriteLine("null"); to print 4 -> 3 -> 2 -> 1 -> null.
  3. Final Answer:

    foreach (int val in arr) list.AddLast(val); Node temp = list.head; while (temp.next != null) temp = temp.next; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.prev; } Console.WriteLine("null"); -> Option B
  4. Quick Check:

    Build forward list + backward traversal from tail [OK]
Hint: Add in order, then move to tail to print backward [OK]
Common Mistakes:
  • Adding elements in reverse order
  • Printing forward instead of backward
  • Using AddFirst which reverses order
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