复杂链表的复制(java版)

来源:互联网 发布:svg.js 对比 编辑:程序博客网 时间:2024/06/07 16:04

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

【节点定义】

/*public class RandomListNode {    int label;    RandomListNode next = null;    RandomListNode random = null;    RandomListNode(int label) {        this.label = label;    }}*/

【解题思路1】
//1.根据每个节点的next指针,先复制链表
//2.遍历新链表,根据原链表random指针,设置新链表中每个节点的random指针
//3.设置random指向位置时,在该方法中采用的是和头结点的相对位置

【源码1】

public class Solution {    public RandomListNode Clone(RandomListNode pHead)    {        if(pHead == null){            return null;        }        RandomListNode inx = pHead;        RandomListNode head = new RandomListNode(inx.label);        RandomListNode index = head;        while(inx.next != null){            inx = inx.next;            RandomListNode tNode = new RandomListNode(inx.label);            index.next = tNode;            index = tNode;        }        inx = pHead;        index = head;        int loc = 0, i=0;        while(inx != null){            RandomListNode temp1 = pHead;            RandomListNode temp2 = head;            if(inx.random == null){                index.random = null;            }else{                loc = randomLoc(temp1, inx.random);      //计算random指向的节点和头结点的相对位置                while(i<loc){                    temp2 = temp2.next;                    i++;                }                index.random = temp2;                i=0;            }             inx = inx.next;            index = index.next;        }        return head;    }    public int randomLoc(RandomListNode h, RandomListNode target){        int loc = 0;        while(h != target){            loc++;            h = h.next;        }        return loc;    }}

a. 注意random指针可能为null。


【解题思路2】
//1.根据next指针复制链表。复制的同时,将原节点和新节点建立对应关系

import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;import java.util.Set;public class Solution {    public RandomListNode Clone(RandomListNode pHead)    {        HashMap<RandomListNode,RandomListNode> map = new HashMap<RandomListNode,RandomListNode>();        RandomListNode p = pHead;        RandomListNode q = new RandomListNode(-1);        while(p!=null){            RandomListNode t = new RandomListNode(p.label);            map.put(p, t);            p = p.next;            q.next = t;            q = t;        }        Set<Entry<RandomListNode,RandomListNode>> set = map.entrySet();                Iterator<Entry<RandomListNode,RandomListNode>> it = set.iterator();                while(it.hasNext()){            Entry<RandomListNode, RandomListNode> next = it.next();                        next.getValue().random = map.get(next.getKey().random);        }        return map.get(pHead);    }}

a. random指针可能为null。
b.next.getValue().random = map.get(next.getKey().random); 是设置新链表random指针的实现部分。
c. 若当前节点的random为null,则有map.get(null) 的情况出现,此时结果为null, 符合要求。


【解题思路3】
//1.根据原链表中每个节点N创建对应的新节点N’。并把N’链接到N的后面。
//2.设置复制出来的节点random。假设N的random指向节点S,因为N’是N的下一个节点,所以S’也应该为S的下一个节点。
//3.把链表拆分。奇数位置的节点链接起来就是原始的链表。偶数位置的节点链接起来,就是新生成的链表。

这里写图片描述

【源码3】

链接:https://www.nowcoder.com/questionTerminal/f836b2c43afc4b35ad6adc41ec941dba来源:牛客网public class Solution {    public RandomListNode Clone(RandomListNode pHead){        if(pHead==null)            return null;        RandomListNode pCur = pHead;        //复制next 如原来是A->B->C 变成A->A'->B->B'->C->C'        while(pCur!=null){            RandomListNode node = new RandomListNode(pCur.label);            node.next = pCur.next;            pCur.next = node;            pCur = node.next;        }        pCur = pHead;        //复制random pCur是原来链表的结点 pCur.next是复制pCur的结点        while(pCur!=null){            if(pCur.random!=null)                pCur.next.random = pCur.random.next;            pCur = pCur.next.next;        }        RandomListNode head = pHead.next;        RandomListNode cur = head;        pCur = pHead;        //拆分链表        while(pCur!=null){            pCur.next = pCur.next.next;            if(cur.next!=null)                cur.next = cur.next.next;            cur = cur.next;            pCur = pCur.next;        }        return head;            }}

a.pCur.next.random = pCur.random.next; 是设置新节点random的实现。
b. 注意设置random时,遍历的实现pCur = pCur.next.next;

0 0
原创粉丝点击