5

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]

5
  • Cool. So, what did you try so far? Commented May 12, 2018 at 9:56
  • var results = ii.forEach(function(obj){ cc(obj.next) }); function cc(req){ arr.push(req.val) } Commented May 12, 2018 at 9:58
  • i'm not getting exact result Commented May 12, 2018 at 9:58
  • 1
    Please update your question to show what you tried, why you tried that, and why it didn't work the way you thought it would. Explaining your idea on how to solve this problem helps everyone else in explaining where your approach broke down. Commented May 12, 2018 at 10:00
  • Without showing any attempt then this looks like "homework". In fact it's the sort of thing we could look at and not really see any "practical usage" and therefore deduce it to in fact be a homework assignment. Whilst there is no "specific rule" against posting homework here, it's not really what a teacher intends when they hand out such work. You'd get more out of it thinking through solving the problem than simply receiving somebody elses answer in order to meet an assignment grade. Commented May 12, 2018 at 10:19

3 Answers 3

8

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)
Sign up to request clarification or add additional context in comments.

Comments

0

A little fancier demonstrating a recursive function call, as may have been the point of the exercise and utilizing Array.reduce().

var ii = {
  "val":"1", "next":{
    "val":"2", "next":{
      "val":"3", "next":{
        "val":"4", "next":{ "val":"5", "next":null }
      }
    }
  }
};

const process = obj => {
  const remap = (acc,[k,v]) => ([
    ...acc,
    ...(( k === 'val') ? v : (v === null)
      ? [] : Object.entries(v).reduce(remap,[]))
  ]);

  return Object.entries(obj).reduce(remap,[]);
};

console.log(process(ii));

Whist fancy, it's not as efficient. So still keeping with "non destructive" you can take a clone of the object and still recurse:

var ii = {
  "val":"1", "next":{
    "val":"2", "next":{
      "val":"3", "next":{
        "val":"4", "next":{ "val":"5", "next":null }
      }
    }
  }
};


const process = obj => (({ val, next }) => 
  [ val, ...((next !== null) ? process(next): []) ])(obj);

console.log(process(ii));

Even cleaner and effectively just as fast as the while, and of course "non-destructive" to the original content since it's all locally scoped.

Comments

0

You can create an empty array and push the data into it while going through the list. I am using JavaScript to solve your problem.

let ii = {"val":"1","next":{"val":"2","next":{"val":"3","next":{"val":"4","next":{"val":"5","next":null}}}}};
let array = [];

//Your linked list
let head = ii;
//Until the list data reaches null continue doing the codes below
while(head !== null){
   //Get the data from list and push it into your array.
   array.push(head.val);
   //after pushing data move to the next list by changing head position
   head = head.next;
}
//return the array after all data is pushed in
return array;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.