Bird
Raised Fist0
DSA C#programming~20 mins

Insert at End of Doubly Linked List 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
What is the output after inserting 4 at the end?
Given the doubly linked list 1 <-> 2 <-> 3 <-> null, what will be the printed list after inserting 4 at the end?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

class DoublyLinkedList {
    public Node head;

    public void InsertAtEnd(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 PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data);
            if (temp.next != null) System.Console.Write(" <-> ");
            temp = temp.next;
        }
        System.Console.WriteLine(" -> null");
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtEnd(1);
dll.InsertAtEnd(2);
dll.InsertAtEnd(3);
dll.InsertAtEnd(4);
dll.PrintList();
A1 <-> 2 <-> 3 -> null
B4 <-> 1 <-> 2 <-> 3 -> null
C1 <-> 2 <-> 3 <-> 4 -> null
D1 <-> 2 <-> 3 <-> 4 <-> null
Attempts:
2 left
💡 Hint
Remember, inserting at the end means the new node comes after the last node.
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting 10 at the end of an empty list?
Given an empty doubly linked list, what will be the printed list after inserting 10 at the end?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

class DoublyLinkedList {
    public Node head;

    public void InsertAtEnd(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 PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data);
            if (temp.next != null) System.Console.Write(" <-> ");
            temp = temp.next;
        }
        System.Console.WriteLine(" -> null");
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtEnd(10);
dll.PrintList();
Anull
B10 -> null
C10 <-> null
Dnull -> 10
Attempts:
2 left
💡 Hint
When the list is empty, the new node becomes the head.
🔧 Debug
advanced
2:00remaining
What error occurs when inserting at end if prev pointer is not set?
Consider this InsertAtEnd method missing the line that sets newNode.prev = temp. What error or problem will occur when traversing backwards from the last node?
DSA C#
public void InsertAtEnd(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: newNode.prev = temp;
}
ACompile-time error due to missing assignment
BNo error, list works fine both ways
CStackOverflowException due to infinite loop
DNullReferenceException when traversing backwards from last node
Attempts:
2 left
💡 Hint
Think about what happens if the previous pointer is not set for the new node.
🧠 Conceptual
advanced
1:30remaining
Why is it necessary to traverse to the end before inserting?
In a doubly linked list, why do we need to traverse from head to the last node before inserting a new node at the end?
ABecause the list does not keep a reference to the last node
BBecause the new node must be inserted at the head first
CBecause traversal updates the data values of nodes
DBecause the list is circular and needs full traversal
Attempts:
2 left
💡 Hint
Think about what information the list stores about its nodes.
❓ Predict Output
expert
2:30remaining
What is the output after multiple insertions and one deletion?
Given the code below, what is the printed list after inserting 5, 10, 15 at the end and then deleting the node with data 10?
DSA C#
class Node {
    public int data;
    public Node next;
    public Node prev;
    public Node(int d) { data = d; next = null; prev = null; }
}

class DoublyLinkedList {
    public Node head;

    public void InsertAtEnd(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 DeleteNode(int data) {
        Node temp = head;
        while (temp != null && temp.data != data) {
            temp = temp.next;
        }
        if (temp == null) return; // Not found
        if (temp.prev != null) temp.prev.next = temp.next;
        else head = temp.next;
        if (temp.next != null) temp.next.prev = temp.prev;
    }

    public void PrintList() {
        Node temp = head;
        while (temp != null) {
            System.Console.Write(temp.data);
            if (temp.next != null) System.Console.Write(" <-> ");
            temp = temp.next;
        }
        System.Console.WriteLine(" -> null");
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtEnd(5);
dll.InsertAtEnd(10);
dll.InsertAtEnd(15);
dll.DeleteNode(10);
dll.PrintList();
A5 <-> 15 -> null
B5 <-> 10 <-> 15 -> null
C10 <-> 15 -> null
D5 -> null
Attempts:
2 left
💡 Hint
Deleting 10 removes that node and links 5 directly to 15.

Practice

(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To remove the first node
B. To find the middle node
C. To reverse the list
D. To add a new element after the last node

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
  2. Step 2: Purpose of insertion at the end

    Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
  3. Final Answer:

    To add a new element after the last node -> Option D
  4. Quick Check:

    Insert at end = add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insertion at end with deletion
  • Thinking insertion reverses the list
  • Assuming insertion happens at the start
2. Which of the following is the correct syntax to create a new node in C# for a doubly linked list?
easy
A. Node newNode = new Node(data);
B. Node newNode = Node(data);
C. newNode = Node.new(data);
D. Node newNode = new Node[];

Solution

  1. Step 1: Understand object creation in C#

    In C#, objects are created using the 'new' keyword followed by the constructor call.
  2. Step 2: Apply to Node creation

    Creating a new node requires calling the Node constructor with data inside parentheses after 'new'.
  3. Final Answer:

    Node newNode = new Node(data); -> Option A
  4. Quick Check:

    Use 'new' keyword with constructor call [OK]
Hint: Use 'new' keyword with constructor parentheses [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using array syntax incorrectly
  • Calling constructor without 'new'
3. Given the following code snippet, what will be the printed output after inserting 30 at the end of the doubly linked list?
class Node {
  public int data;
  public Node next, prev;
  public Node(int d) { data = d; next = prev = null; }
}

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

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtEnd(10);
dll.InsertAtEnd(20);
dll.InsertAtEnd(30);
dll.PrintList();
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. 10 -> 20 -> null
D. null

Solution

  1. Step 1: Trace InsertAtEnd calls

    First insert 10: list is 10 -> null. Then insert 20: list is 10 -> 20 -> null. Then insert 30: list is 10 -> 20 -> 30 -> null.
  2. Step 2: PrintList outputs nodes in order

    PrintList prints each node's data followed by '->', ending with 'null'. So output is '10 -> 20 -> 30 -> null'.
  3. Final Answer:

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

    Insertion at end appends nodes in order [OK]
Hint: Insert at end appends nodes; print shows order [OK]
Common Mistakes:
  • Confusing insertion order
  • Printing in reverse order
  • Missing 'null' at end
4. Identify the error in the following InsertAtEnd method for a doubly linked list:
public void InsertAtEnd(int data) {
  Node newNode = new Node(data);
  if (head == null) {
    head = newNode;
  }
  Node temp = head;
  while (temp.next != null) {
    temp = temp.next;
  }
  temp.next = newNode;
  newNode.prev = temp;
}
medium
A. Loop condition should be temp != null
B. Missing return after setting head when list is empty
C. Incorrect assignment of newNode.prev
D. Node constructor is called incorrectly

Solution

  1. Step 1: Analyze empty list case

    If head is null, head is set to newNode but code continues to loop and assign pointers again, causing newNode to link to itself.
  2. Step 2: Fix by adding return after setting head

    Adding 'return;' after 'head = newNode;' stops further code execution, preventing incorrect pointer assignments.
  3. Final Answer:

    Missing return after setting head when list is empty -> Option B
  4. Quick Check:

    Return after empty list insert prevents errors [OK]
Hint: Add return after empty list insertion [OK]
Common Mistakes:
  • Not returning after setting head
  • Wrong loop condition
  • Misassigning prev pointer
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25. You want to insert 35 at the end. After insertion, which of the following correctly describes the pointers of the new node and the previous last node?
hard
A. newNode.prev points to null; node with 25.next points to newNode
B. newNode.next points to node with 25; node with 25.prev points to newNode
C. newNode.prev points to node with 25; node with 25.next points to newNode
D. newNode.prev and newNode.next both point to null; node with 25.next points to null

Solution

  1. Step 1: Understand pointer updates when inserting at end

    The new node's prev pointer should point to the current last node (25), and the current last node's next pointer should point to the new node.
  2. Step 2: Confirm new node's next pointer

    The new node's next pointer should be null because it is now the last node.
  3. Final Answer:

    newNode.prev points to node with 25; node with 25.next points to newNode -> Option C
  4. Quick Check:

    Prev points backward; next points forward [OK]
Hint: New node prev = old last; old last next = new node [OK]
Common Mistakes:
  • Swapping prev and next pointers
  • Setting newNode.prev to null
  • Not updating old last node's next pointer
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