Created
May 20, 2021 06:39
-
-
Save jrainlau/5d143dadb9f59a37cd36e0aa668b0a00 to your computer and use it in GitHub Desktop.
Array|Link
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
function ListNode(val, next) { | |
this.val = (val === undefined ? 0 : val) | |
this.next = (next === undefined ? null : next) | |
} | |
function List2Array (list, arr = []) { | |
arr.push(list.val) | |
if (list.next instanceof ListNode) { | |
List2Array(list.next, arr) | |
} | |
return arr | |
} | |
function Array2List(arr, list = new ListNode()) { | |
if (arr.length) { | |
list.val = arr.shift() | |
list.next = arr.length ? new ListNode() : null | |
Array2List(arr, list.next) | |
} | |
return list | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment