Linked List nested Object
Input Should be like this
var ii = {"val":"1","next":{"val":"2","next":{"val":"3","next":{"val":"4","next":{"val":"5","next":null}}}}};
Output should be like [1,2,3,4,5]
When converting an object to an array, we'll use the . entries() method from the Object class. This will convert our object to an array of arrays. Each nested array is a two-value list where the first item is the key and the second item is the value.
An array of linked lists is an important data structure that can be used in many applications. Conceptually, an array of linked lists looks as follows. An array of linked list is an interesting structure as it combines a static structure (an array) and a dynamic structure (linked lists) to form a useful data structure.
As stated earlier, a list node contains two items: the data and the pointer to the next node. We can implement a list node in JavaScript as follows: class ListNode { constructor(data) { this. data = data this.
Solution
var ii = { "val": "1", "next": { "val": "2", "next": { "val": "3", "next": { "val": "4", "next": { "val": "5", "next": null } } } } };
var arr = [ii.val]
while(ii.next !== null){
ii = ii.next;
arr.push(ii.val)
}
console.log(arr)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With