Created
July 11, 2020 19:37
-
-
Save bluepnume/3b9ecdf8daf268df77215f339caeb256 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
[ ['A', 'B'], ['B', 'C'], ['C', 'D'], ['D'] ] | |
[ 'A', 'B', 'C', 'D' ] | |
-> | |
{ id: 'A', next: { id: 'B', next: { id: 'C', next: { id: 'D', next: null } } } } | |
*/ | |
/* | |
type Node = { | |
id : string, | |
next : ?Node | |
}; | |
type Nodes = { | |
[string]: Node | |
} | |
*/ | |
function arrayToLinkedList(arr) { | |
if (!Array.isArray(arr)) { | |
throw new Error(`Expected array`); | |
} | |
const nodes = {}; // Nodes | |
let firstNode; | |
for (const [ first, second ] of arr) { | |
nodes[first] = nodes[first] || { id: first }; | |
nodes[second] = nodes[second] || { id: second }; | |
nodes[first].next = nodes[second]; | |
firstNode = firstNode || nodes[first]; | |
} | |
return firstNode; | |
} | |
function recursiveArrayToLinkedList(arr) { | |
if (!Array.isArray(arr)) { | |
throw new Error(`Expected array`); | |
} | |
const nodes = {}; // Nodes | |
let firstNode; | |
const innerRecursiveArrayToLinkedList = (arr) => { | |
if (arr.length === 0) { | |
return; | |
} | |
const [ first, second ] = arr[0]; | |
nodes[first] = nodes[first] || { id: first }; | |
nodes[second] = nodes[second] || { id: second }; | |
nodes[first].next = nodes[second]; | |
firstNode = firstNode || nodes[first]; | |
innerRecursiveArrayToLinkedList(arr.slice(1)) | |
}; | |
innerRecursiveArrayToLinkedList(arr); | |
return firstNode; | |
} | |
function recursiveArrayToLinkedList2(arr, nodes = {}) { | |
if (!Array.isArray(arr)) { | |
throw new Error(`Expected array`); | |
} | |
if (arr.length === 0) { | |
return; | |
} | |
const [ first, second ] = arr[0]; | |
nodes[first] = nodes[first] || { id: first }; | |
nodes[second] = nodes[second] || { id: second }; | |
nodes[first].next = nodes[second]; | |
recursiveArrayToLinkedList2(arr.slice(1), nodes); | |
return nodes[first]; | |
} | |
function recursiveArrayToLinkedList3(arr) { | |
if (!Array.isArray(arr)) { | |
throw new Error(`Expected array`); | |
} | |
if (arr.length === 0) { | |
return; | |
} | |
const first = arr[0]; | |
const firstNode = { id: first }; | |
firstNode.next = recursiveArrayToLinkedList3(arr.slice(1)); | |
return firstNode; | |
} | |
function loopArrayToLinkedList3(arr, loop = false) { | |
if (!Array.isArray(arr)) { | |
throw new Error(`Expected array`); | |
} | |
let firstNode; | |
let previousNode; | |
let newNode; | |
for (const item of arr) { | |
newNode = { id: item }; | |
firstNode = firstNode || newNode; | |
if (previousNode) { | |
previousNode.next = newNode; | |
} | |
previousNode = newNode; | |
}; | |
if (loop) { | |
newNode.next = firstNode; | |
} | |
return firstNode; | |
} | |
const testCases = [ | |
{ | |
input: [ 'A', 'B', 'C', 'D' ], | |
output: { id: 'A', next: { id: 'B', next: { id: 'C', next: { id: 'D' } } } } | |
}, | |
{ | |
input: [], | |
output: undefined | |
} | |
]; | |
for (const { input, output } of testCases) { | |
const result = loopArrayToLinkedList3(input, true); | |
const serializedInput = JSON.stringify(input, null, 4); | |
const serializedOutput = JSON.stringify(output, null, 4); | |
const serializedResult = JSON.stringify(result, null, 4); | |
if (serializedResult !== serializedOutput) { | |
throw new Error(`Expected linked list for ${ serializedInput } to be ${ serializedOutput } but got ${ serializedResult }`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment