白衣染霜華 發表於 2019-10-15 20:30:00

用js刷剑指offer(复杂链表的复制)

<h2 id="toc_0">题目描述</h2>

<p>输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)</p>

<h2 id="toc_1">思路</h2>

<p><img src="https://picture-bed-1255998923.cos.ap-chengdu.myqcloud.com/2019/10/15/15711425294464.jpg"></p>

<p>牛客网链接</p>

<h2 id="toc_2">js代码</h2>

<pre><code class="language-javascript">/*function RandomListNode(x){
    this.label = x;
    this.next = null;
    this.random = null;
}*/
function Clone(pHead)
{
    // write code here
    if (!pHead) return null
    let p = pHead
    while (p) {
      let node = new RandomListNode(p.label)
      node.next = p.next
      p.next = node
      p = p.next.next
    }
    p = pHead
    while (p) {
      if (p.random) p.next.random = p.random
      else p.next.random = null
      p = p.next.next
    }
   
    p = pHead.next
    let root = new RandomListNode(0)
    let q = root
    while (p) {
      q.next = p
      q = q.next
      if (p.next) p = p.next.next
      else break
    }
    q.next = null
    return root.next
}```
</code></pre><br><br>
来源:https://www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-fu-za-lian-biao-de-fu-zh.html
頁: [1]
查看完整版本: 用js刷剑指offer(复杂链表的复制)