Create and Initialize Doubly Linked List in DSA C# - Time & Space Complexity
We want to understand how long it takes to create and set up a doubly linked list when given some data.
How does the time needed change as the number of items grows?
Analyze the time complexity of the following code snippet.
public class Node {
public int Value;
public Node Prev;
public Node Next;
public Node(int value) {
Value = value;
Prev = null;
Next = null;
}
}
public Node CreateDoublyLinkedList(int[] values) {
if (values.Length == 0) return null;
Node head = new Node(values[0]);
Node current = head;
for (int i = 1; i < values.Length; i++) {
Node newNode = new Node(values[i]);
current.Next = newNode;
newNode.Prev = current;
current = newNode;
}
return head;
}
This code creates a doubly linked list from an array of integers by linking nodes forward and backward.
- Primary operation: The for-loop that creates and links each new node.
- How many times: It runs once for each element after the first, so roughly n-1 times for n elements.
Each new item requires creating a node and linking it, so the work grows steadily as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 node creations and links |
| 100 | About 99 node creations and links |
| 1000 | About 999 node creations and links |
Pattern observation: The number of operations grows directly with the number of items.
Time Complexity: O(n)
This means the time to create the list grows in a straight line with the number of elements.
[X] Wrong: "Creating a doubly linked list takes constant time because each node is created once."
[OK] Correct: Even though each node is created once, the total time depends on how many nodes there are, so it grows with the list size.
Understanding how list creation time grows helps you explain and reason about data structure setup in interviews confidently.
"What if we changed the input from an array to a linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand doubly linked list structure
A doubly linked list has nodes with pointers to both next and previous nodes.Step 2: Identify traversal capability
This structure allows moving forward and backward through the list easily.Final Answer:
To allow traversal in both forward and backward directions -> Option AQuick Check:
Doubly linked list = bidirectional traversal [OK]
- Confusing doubly linked list with arrays
- Thinking it sorts data automatically
- Assuming it only allows forward traversal
Solution
Step 1: Check node fields for doubly linked list
Node must have data, next pointer, and previous pointer of type Node.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.Final Answer:
public class Node { public int data; public Node next; public Node prev; } -> Option CQuick Check:
Node class with data, next, prev = public class Node { public int data; public Node next; public Node prev; } [OK]
- Using int for next and prev instead of Node
- Missing prev pointer for doubly linked list
- Incorrect access modifiers
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();
}
}Solution
Step 1: Understand AddLast method behavior
AddLast adds nodes at the end, so nodes are 10, then 20, then 30 in order.Step 2: Trace PrintForward output
PrintForward prints from head to last node: 10 -> 20 -> 30 -> null.Final Answer:
10 -> 20 -> 30 -> null -> Option DQuick Check:
Forward print = 10 -> 20 -> 30 -> null [OK]
- Confusing forward and backward traversal
- Missing last node in output
- Printing in reverse order
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
}Solution
Step 1: Review AddLast logic
After finding last node, newNode is linked as next of temp.Step 2: Check doubly linked list pointer updates
For doubly linked list, newNode.prev must point back to temp to maintain backward link.Final Answer:
Missing setting newNode.prev = temp -> Option AQuick Check:
Set newNode.prev to temp for backward link [OK]
- Forgetting to set newNode.prev
- Reassigning head incorrectly
- Skipping loop to find last node
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?Solution
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.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.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 BQuick Check:
Build forward list + backward traversal from tail [OK]
- Adding elements in reverse order
- Printing forward instead of backward
- Using AddFirst which reverses order
Ready to prove it?
Pass the test and this pattern moves unknown
