Node Structure and Pointer Design in DSA Python - Time & Space Complexity
We want to understand how the design of a node and its pointers affects the time it takes to perform operations on linked data structures.
Specifically, how does the way nodes connect influence the speed of traversing or modifying the structure?
Analyze the time complexity of the following node traversal code.
class Node:
def __init__(self, value):
self.value = value
self.next = None
def traverse(head):
current = head
while current is not None:
print(current.value)
current = current.next
This code defines a simple node with a pointer to the next node and traverses the linked list from head to end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to the next using the
nextpointer. - How many times: Once for each node in the list until the end (n times for n nodes).
As the number of nodes grows, the traversal takes longer because it visits each node once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The number of steps grows directly with the number of nodes.
Time Complexity: O(n)
This means the time to traverse the list grows linearly with the number of nodes.
[X] Wrong: "Because each node points only to the next, traversal might be faster than visiting all nodes."
[OK] Correct: Even with simple pointers, you must visit each node once, so time grows with the list size.
Understanding how node pointers affect traversal time helps you explain linked list operations clearly and confidently in interviews.
"What if each node had a pointer to both the next and previous nodes? How would that change the time complexity of traversal?"
Practice
next pointer in a linked list node represent?Solution
Step 1: Understand the role of
Thenextpointernextpointer in a linked list node is used to connect the current node to the following node in the sequence.Step 2: Differentiate from other node parts
The data part stores the value, andnextspecifically points to the next node, not previous or size.Final Answer:
It points to the next node in the list -> Option CQuick Check:
nextpointer = next node address [OK]
next always leads forward in the list [OK]- Confusing
nextwith data storage - Thinking
nextpoints backward - Assuming
nextstores list size
Solution
Step 1: Check correct attribute assignment
class Node: def __init__(self, data): self.data = data self.next = None correctly assignsself.data = dataand initializesself.next = Noneto indicate no next node yet.Step 2: Identify errors in other options
class Node: def __init__(self, data): data = self.data next = None assigns backwards, class Node: def __init__(self): self.data = None self.next = None misses data parameter, class Node: def __init__(self, data, next): self.data = data next = None does not assignself.nextproperly.Final Answer:
class Node: def __init__(self, data): self.data = data self.next = None -> Option AQuick Check:
Correct attribute setup = class Node: def __init__(self, data): self.data = data self.next = None [OK]
self.attribute = value inside __init__ [OK]- Not using
selfto assign attributes - Forgetting to initialize
nextto None - Swapping assignment order
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(10)
second = Node(20)
head.next = second
print(head.next.data)Solution
Step 1: Understand node creation and linking
Two nodes are created: head with data 10, second with data 20. Then,head.nextpoints tosecond.Step 2: Trace the print statement
head.next.dataaccesses the data of the node after head, which issecond, so it prints 20.Final Answer:
20 -> Option AQuick Check:
head.next.data = 20 [OK]
next pointer to get next node's data [OK]- Printing head.data instead of head.next.data
- Confusing
nextpointer with data - Expecting None because of uninitialized pointer
class Node:
def __init__(self, data):
self.data = data
next = None
node1 = Node(5)
print(node1.next)Solution
Step 1: Check attribute assignment in
The code assigns__init__next = Nonewithoutself., sonextis a local variable, not an attribute.Step 2: Understand consequence on print
Sincenode1.nextwas never set, it raises an AttributeError or prints None if accessed differently.Final Answer:
Thenextpointer is not assigned toself-> Option DQuick Check:
Missingself.causes attribute error [OK]
self. to create object attributes [OK]- Forgetting
self.beforenext - Assuming local variable becomes attribute
- Ignoring error from missing attribute
Solution
Step 1: Understand correct node linking order
To create a linked list 1 -> 2 -> 3,n1.nextshould point ton2, andn2.nextton3. The last noden3.nextshould be None.Step 2: Check each option's linking
n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = n2 n2.next = n3 n3.next = None correctly links nodes in order. The other options link nodes in wrong order or reverse.Final Answer:
n1.next = n2; n2.next = n3; n3.next = None -> Option BQuick Check:
Correct forward linking = n1 = Node(1) n2 = Node(2) n3 = Node(3) n1.next = n2 n2.next = n3 n3.next = None [OK]
- Linking nodes in reverse order
- Not setting last node's next to None
- Mixing node references
Ready to prove it?
Pass the test and this pattern moves unknown
