为什么我的算法说[圆形]? (NodeJS简单算法)

问题描述 投票:4回答:1
var swapPairs = function(head) {
    if (head == null || head.next == null) {
        return; 
    }
    let oldHead = head;
    let nextHead = head.next;
    oldHead.next = swapPairs(nextHead.next);
    head.next = oldHead;
    return head;
};

console.log(swapPairs(list.head));

任何线索为什么Node JS响应每个头但是响应下一个值“[circular]”?

例如:{value:16,next:[Circular]}

javascript node.js algorithm object circular-dependency
1个回答
2
投票

因为它是圆形的 - 它是无限嵌套的:

value: 16,
next: {
    next: {
        next: {...}
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.