Push Using Linked List Node in DSA C# - Time & Space Complexity
We want to understand how long it takes to add a new item to the start of a linked list.
How does the time needed change as the list grows?
Analyze the time complexity of the following code snippet.
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedList {
public Node head;
public void Push(int newData) {
Node newNode = new Node(newData);
newNode.next = head;
head = newNode;
}
}
This code adds a new node at the beginning of a linked list by adjusting pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: This happens once per push call, no loops or recursion involved.
Each push adds one node by changing a few pointers, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (create node, set next, update head) |
| 100 | 3 (same steps, list size doesn't matter) |
| 1000 | 3 (still just these steps) |
Pattern observation: The number of steps stays the same no matter how large the list grows.
Time Complexity: O(1)
This means adding a new node at the start takes the same small amount of time no matter how big the list is.
[X] Wrong: "Adding a node takes longer as the list gets bigger because we have to move through the list."
[OK] Correct: We only change the first node's pointer, so we never walk through the list. The size does not affect the time.
Knowing that adding at the start of a linked list is quick helps you explain efficient data structure choices in interviews.
"What if we had to add a node at the end of the linked list instead? How would the time complexity change?"
Practice
push operation do in a linked list when using nodes?Solution
Step 1: Understand push operation in linked list
The push operation inserts a new node at the start of the linked list.Step 2: Effect of push on list structure
The new node becomes the head, and its next pointer points to the old head.Final Answer:
Adds a new node at the beginning of the list -> Option AQuick Check:
Push means insert at head [OK]
- Confusing push with pop operation
- Thinking push adds at the end
- Assuming push deletes nodes
Solution
Step 1: Create new node with data
Usenew Node(data)to create a node with the given data.Step 2: Link new node to current head and update head
SetnewNode.next = headand thenhead = newNodeto push at front.Final Answer:
Node newNode = new Node(data); newNode.next = head; head = newNode; -> Option BQuick Check:
New node points to old head, then head updated [OK]
- Assigning head before setting newNode.next
- Not initializing new node with data
- Swapping head and newNode incorrectly
Node head = null; head = new Node(10); head.next = new Node(20); Node newNode = new Node(5); newNode.next = head; head = newNode;
Print the list from head to end.
Solution
Step 1: Initial list creation
Head points to node 10, which points to node 20, then null.Step 2: Push new node with value 5
New node 5 points to old head (10), then head updated to new node 5.Final Answer:
5 -> 10 -> 20 -> null -> Option DQuick Check:
New node added at front [OK]
- Appending new node at end instead of front
- Not updating head to new node
- Confusing node order after push
public void Push(int data) {
Node newNode = new Node(data);
newNode.next = null;
head = newNode;
}Solution
Step 1: Analyze newNode.next assignment
SettingnewNode.next = nulldisconnects new node from existing list.Step 2: Correct linking for push operation
New node's next should point to current head to keep list intact before updating head.Final Answer:
newNode.next should point to current head before updating head -> Option AQuick Check:
Link new node to old head before head update [OK]
- Setting newNode.next to null breaks list
- Not updating head after push
- Confusing order of assignments
Solution
Step 1: Initial list and first push
Start with 30 -> 20 -> 10 -> null. Push 40: new head is 40, list is 40 -> 30 -> 20 -> 10 -> null.Step 2: Second push with 50
Push 50: new head is 50, list becomes 50 -> 40 -> 30 -> 20 -> 10 -> null.Final Answer:
50 -> 40 -> 30 -> 20 -> 10 -> null -> Option CQuick Check:
Push adds nodes at front in reverse order [OK]
- Assuming push adds at end
- Mixing order of pushed nodes
- Not updating head after each push
Ready to prove it?
Pass the test and this pattern moves unknown
