Here's some quick code I used to set up a quick array of linked lists for a leetcode problem - Merge K Sorted Lists.

Converting arrays to singly-linked LinkedLists (without head or tail).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

let ListNode = function(value) {
this.value = value;
this.next = null;

this.connect = (node) => {
this.next = node;
return this;
}
}

let convertArray = (array) => {
let nodeMap = array.map((x) => new ListNode(x));

return nodeMap.map((x, i, arr) => {
if (arr[i + 1]) return arr[i].connect(arr[i + 1])
})[0]
}

// Input:
// [
// 1->4->5,
// 1->3->4,
// 2->6
// ]
// Output: 1->1->2->3->4->4->5->6

let testArray = [[1, 4, 5], [1, 3, 4], [2, 6]]
testArray = testArray.map((x) => convertArray(x));