-
Notifications
You must be signed in to change notification settings - Fork 266
Find Length of Doubly Linked List from Any Node
Unit 5 Session 2 (Click for link to problem statements)
TIP102 Unit 5 Session 2 Standard (Click for link to problem statements)
- 💡 Difficulty: Medium
- ⏰ Time to complete: 15-20 mins
- 🛠️ Topics: Linked Lists, Traversal
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What happens if the node is
None
?- The function should return
0
.
- The function should return
- What happens if the doubly linked list contains only one node?
- The function should return
1
.
- The function should return
HAPPY CASE
Input: node is the node with value 6 in the list 3 <-> 5 <-> 6 <-> 7
Output: 4
Explanation: The function calculates the length of the entire doubly linked list.
EDGE CASE
Input: node = None
Output: 0
Explanation: When the node is `None`, the function returns `0`.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Linked List length problems, we want to consider the following approaches:
- Traversal in both directions to find the head and tail
- Counting nodes while traversing the list
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the list from the given node to find the head, then traverse the entire list from head to tail to count the number of nodes.
Traverse back to the head via prev.
Traverse forward via next to count nodes.
- Forgetting to handle the case where the node is
None
. - Not correctly traversing to the start of the list before counting.
Implement the code to solve the algorithm.
def get_length(node):
if node is None:
return 0
# Find the start of the list
start = node
while start.prev:
start = start.prev
# Traverse from start to end, counting nodes
length = 0
current = start
while current:
length += 1
current = current.next
return length
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Verify the length returned matches the expected length of the doubly linked list for different starting nodes.
Example:
class Node:
def __init__(self, value, next=None, prev=None):
self.value = value
self.next = next
self.prev = prev
# Build: 3 <-> 5 <-> 6 <-> 7
n1 = Node(3)
n2 = Node(5)
n3 = Node(6)
n4 = Node(7)
n1.next, n2.prev = n2, n1
n2.next, n3.prev = n3, n2
n3.next, n4.prev = n4, n3
# Test from any node:
print(get_length(n3)) # Expected: 4 (start in the middle, value 6)
print(get_length(n1)) # Expected: 4 (head)
print(get_length(n4)) # Expected: 4 (tail)
print(get_length(None)) # Expected: 0
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
- Time Complexity: O(N) where N is the number of nodes in the list, as we need to traverse the entire list.
- Space Complexity: O(1) because we are only using a constant amount of extra space for the pointers.