Skip to content

Doubly Linked List to Array

01 · Question

Given a non-null node from a doubly linked list (which might not be the head), return an array of values from head to tail.

02 · Solution

Reference solution

1def to_array(node: 'DoublyNode') -> list[int]:
2 cur = node
3 while cur.prev:
4 cur = cur.prev
5 res = []
6 while cur:
7 res.append(cur.val)
8 cur = cur.next
9 return res