Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Nested Object Linked List into Array Node js

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]

like image 830
Chandan Kumar Avatar asked May 12 '18 09:05

Chandan Kumar


People also ask

Which method is used to convert nested arrays to objects?

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.

Can we store linked list in array?

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.

What is ListNode in JavaScript?

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.


1 Answers

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)
like image 133
Chandan Kumar Avatar answered Sep 20 '22 01:09

Chandan Kumar