-
Notifications
You must be signed in to change notification settings - Fork 243
Doubly Linked List
Sar Champagne Bielert edited this page Apr 20, 2024
·
1 revision
Unit 5 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What should be the relationship between each
Node
in a doubly linked list?- Each node should point to both the next and the previous nodes in the list, facilitating bidirectional traversal.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Construct a doubly linked list from the given array of values, ensuring each node is correctly linked in both directions.
1) Create nodes for "Poliwag", "Poliwhirl", and "Poliwrath".
2) Set the `next` attribute of each node to link to the subsequent node.
3) Set the `prev` attribute of each node to link to the previous node.
4) Ensure that the first node's `prev` is `None` and the last node's `next` is `None`.
- Not setting the
prev
attribute correctly, which could lead to incorrect or incomplete backward links. - Forgetting to handle the ends of the list where
prev
ornext
should beNone
.
poliwag = Node("Poliwag")
poliwhirl = Node("Poliwhirl")
poliwrath = Node("Poliwrath")
poliwag.next = poliwhirl
poliwhirl.next = poliwrath
poliwrath.prev = poliwhirl
poliwhirl.prev = poliwag