A doubly linked list is a data structure in which each node has a value
as well as "links" to both the previous
and next nodes
in the list. For example, consider the following nodes with values 12, 99, and 37:
Here, the nodes with values 12 and 99 point to their respective next
nodes, with values 99 and 37. The node with value 37 has no next
pointer because it's the last node in the list. Likewise, the nodes with values 99 and 37 point to their respective previous
nodes, 12 and 99, but 12 has no previous
pointer because it's the first node in the list.
The setup
In practice, a node's "links" are implemented as pointers to the previous and next node's locations in memory. For our purposes, the "memory" will be an array of nodes and a node's location will be its index in the array. A node can be thought of as a 3-tuple of the form ( prev value next )
. The above example, then, might look like this:
But it might look like this, instead:
Starting at any node, you can follow previous
links (shown as the origins of the red arrows) to get to the nodes that precede it and next
links (green arrows) to find subsequent nodes in order to get all of the nodes' values in order: [12, 99, 37]
.
The first diagram above could be represented in an array as [[null, 12, 1], [0, 99, 2], [1, 37, null]]
. The second, then, would be [[2, 99, 1], [0, 37, null], [null, 12, 0]]
.
The challenge
Write a program that takes as input an array of nodes and the index of a node and returns, in list order, the values of the nodes in the same doubly linked list.
A complication
The "memory" won't always contain the nodes of just one list. It might contain several lists:
The above array contains three doubly linked lists, color-coded for your convenience:
The nodes at indexes
7
,10
,1
,4
,3
,12
(showing onlynext
links to reduce clutter; click to enlarge):Given this array and any of these indexes, your program should return, in order, the values
[0, 1, 1, 2, 3, 5, 8]
.The node at index
9
:Given the index
9
, your program should return[99]
.The nodes at indexes
11
,8
,0
,6
,2
:Given one of these indexes, it should return
[2, 3, 5, 7, 11]
.
Rules
Input
Your program will receive as input:
A list of 𝒏 nodes (3-tuples as described above), where 1 ≤ 𝒏 ≤ 1,000, in any convenient format, e.g. an array of arrays, a "flat" array of integers with length 3𝒏, etc.
The 3-tuples' elements may be in any order:
( prev value next )
,( next prev value )
, etc. For each node,prev
andnext
will benull
(or another convenient value, e.g.-1
), indicating the first or last node in a doubly linked list, or a valid index of the list, either 0- or 1-based as is convenient.value
will be a signed 32-bit integer or the largest integer type your language supports, whichever is smaller.The index 𝒑 of a node in the list (1). The indicated node may be the first node in a doubly linked list, the last node, a middle node, or even the only node.
The input list (1) may contain pathological data (e.g. cycles, nodes pointed to by multiple other nodes, etc.), but the input index (2) will always point to a node from which a single, well-formed output can be deduced.
Output
Your program should output the values of the nodes of the doubly linked list of which the node at index 𝒑 is a member, in list order. Output can be in any convenient format, but its data must include only the node value
s.
Winning
This is code-golf. Shortest answer in bytes wins. Standard loopholes apply.
Test cases
Below, each test case is of the form:
X)
prev value next, prev value next, ...
index
value value value ...
...where X
is a letter to identify the test case, the second line is the input list, the third line is the 0-based input index, and the fourth line is the output.
A) null 12 1, 0 99 2, 1 37 null
1
12 99 37
B) 2 99 1, 0 37 null, null 12 0
1
12 99 37
C) 8 5 6, 10 1 4, 6 11 null, 4 3 12, 1 2 3, 12 8 null, 0 7 2, null 0 10, 11 3 0, null 99 null, 7 1 1, null 2 8, 3 5 5
4
0 1 1 2 3 5 8
D) 8 5 6, 10 1 4, 6 11 null, 4 3 12, 1 2 3, 12 8 null, 0 7 2, null 0 10, 11 3 0, null 99 null, 7 1 1, null 2 8, 3 5 5
0
2 3 5 7 11
E) 8 5 6, 10 1 4, 6 11 null, 4 3 12, 1 2 3, 12 8 null, 0 7 2, null 0 10, 11 3 0, null 99 null, 7 1 1, null 2 8, 3 5 5
9
99
F) 13 80 18, 18 71 null, 5 10 19, 12 1 8, 19 21 null, 31 6 2, 17 5 26, 26 0 30, 3 -1 25, null 1 23, 27 6 17, 14 1 24, 28 -1 3, null 80 0, 20 4 11, 33 6 29, 24 9 33, 10 7 6, 0 67 1, 2 15 4, 32 1 14, null 1 31, 29 3 null, 9 -1 28, 11 5 16, 8 1 null, 6 3 7, null 8 10, 23 1 12, 15 5 22, 7 9 null, 21 3 5, null 3 20, 16 2 15
18
80 80 67 71
G) 13 80 18, 18 71 null, 5 10 19, 12 1 8, 19 21 null, 31 6 2, 17 5 26, 26 0 30, 3 -1 25, null 1 23, 27 6 17, 14 1 24, 28 -1 3, null 80 0, 20 4 11, 33 6 29, 24 9 33, 10 7 6, 0 67 1, 2 15 4, 32 1 14, null 1 31, 29 3 null, 9 -1 28, 11 5 16, 8 1 null, 6 3 7, null 8 10, 23 1 12, 15 5 22, 7 9 null, 21 3 5, null 3 20, 16 2 15
8
1 -1 1 -1 1 -1 1
H) 13 80 18, 18 71 null, 5 10 19, 12 1 8, 19 21 null, 31 6 2, 17 5 26, 26 0 30, 3 -1 25, null 1 23, 27 6 17, 14 1 24, 28 -1 3, null 80 0, 20 4 11, 33 6 29, 24 9 33, 10 7 6, 0 67 1, 2 15 4, 32 1 14, null 1 31, 29 3 null, 9 -1 28, 11 5 16, 8 1 null, 6 3 7, null 8 10, 23 1 12, 15 5 22, 7 9 null, 21 3 5, null 3 20, 16 2 15
4
1 3 6 10 15 21
I) 13 80 18, 18 71 null, 5 10 19, 12 1 8, 19 21 null, 31 6 2, 17 5 26, 26 0 30, 3 -1 25, null 1 23, 27 6 17, 14 1 24, 28 -1 3, null 80 0, 20 4 11, 33 6 29, 24 9 33, 10 7 6, 0 67 1, 2 15 4, 32 1 14, null 1 31, 29 3 null, 9 -1 28, 11 5 16, 8 1 null, 6 3 7, null 8 10, 23 1 12, 15 5 22, 7 9 null, 21 3 5, null 3 20, 16 2 15
14
3 1 4 1 5 9 2 6 5 3
J) 13 80 18, 18 71 null, 5 10 19, 12 1 8, 19 21 null, 31 6 2, 17 5 26, 26 0 30, 3 -1 25, null 1 23, 27 6 17, 14 1 24, 28 -1 3, null 80 0, 20 4 11, 33 6 29, 24 9 33, 10 7 6, 0 67 1, 2 15 4, 32 1 14, null 1 31, 29 3 null, 9 -1 28, 11 5 16, 8 1 null, 6 3 7, null 8 10, 23 1 12, 15 5 22, 7 9 null, 21 3 5, null 3 20, 16 2 15
17
8 6 7 5 3 0 9
K) 4 11 0, null 22 3, null 33 3, 1 44 4, 3 55 null, 7 66 7, 6 77 6
3
22 44 55
L) null -123 null
0
-123