严格使用for循环创建嵌套对象

时间:2022-01-22 07:19:01

So, I already know how to do this using recursion. However, I wanted to create my singly linked list strictly using a for-loop. I need to write ansList then ansList.next then ansList.next.next ... etc i.e. I want something like this)

所以,我已经知道如何使用递归来做到这一点。但是,我想严格使用for循环创建我的单链表。我需要写ansList然后ansList.next然后ansList.next.next ...等我想要这样的东西)

let ansList = new ListNode(ansArr[0]);
let n = next;
for (let i =1; i<=ansArr.length;i++) {      
    let x = ansList + n**i;
    x = new ListNode(ansArr[i]);
    };

1 个解决方案

#1


0  

ListNode = function(parent)
{
  this.parent = parent;
}

var first = new ListNode();
var next = first;
for (let i =1; i<=10;i++)
      next = new ListNode(next);

console.log(next);

#1


0  

ListNode = function(parent)
{
  this.parent = parent;
}

var first = new ListNode();
var next = first;
for (let i =1; i<=10;i++)
      next = new ListNode(next);

console.log(next);