Back to all solutions
#3294 - Convert Doubly Linked List to Array II
Problem Description
You are given an arbitrary node from a doubly linked list, which contains nodes that have a next pointer and a previous pointer.
Return an integer array which contains the elements of the linked list in order.
Solution
/**
* // Definition for a _Node.
* function _Node(val,prev,next) {
* this.val = val;
* this.prev = prev;
* this.next = next;
* };
*/
/**
* @param {_Node} node
* @return {number[]}
*/
var toArray = function(node) {
const result = [];
let headNode = node;
while (headNode.prev) {
headNode = headNode.prev;
}
let currentNode = headNode;
while (currentNode) {
result.push(currentNode.val);
currentNode = currentNode.next;
}
return result;
};