解释:
- 复制一条链表。
- 复制的链表中的节点是全新的。
- 新链表的 next 和 random 指针要正确地指向新链表中的节点,而不是原链表中的节点。
方法一:哈希表+一次遍历
我们知道复制一个普通的链表从头到尾挨个复制就行,但由于random随机指针的存在,我们在复制一个结点的时候,并没有办法直接去复制random指向的结点,因为我们并不知道这个random是否会指向我们前面已经复制过的结点,因此,我们需要一个HashMap去记录我们复制过的结点。自然而然想到键值对key是random指向的结点(链表中的任意结点),value是其复制的结点。
class Solution {
public Node copyRandomList(Node head) {
//key-遍历过的结点,value-对应的复制结点
Map<Node, Node> map = new HashMap<>();
//cur-当前遍历结点
Node cur = head;
//复制的链表增加一个哨兵结点
Node result = new Node(-1);
//复制的链表尾指针
Node tail = result;
//遍历原链表
while(cur != null){
//如果当前结点没被复制过
if(!map.containsKey(cur)){
//复制当前结点的值
Node curCopNode = new Node(cur.val);
//将复制的结点添加到复制链表中
tail.next = curCopNode;
//map中添加当前结点的复制记录
map.put(cur, curCopNode);
}else{
//如果当前结点被复制过,说明是前面某个结点的random复制的
//直接将其添加到复制链表中
tail.next = map.get(cur);
}
//复制的链表尾结点后移
tail = tail.next;
//如果当前结点的random指针不为空并且对应的结点没被复制过
if(cur.random != null && !map.containsKey(cur.random)){
//复制random结点的值
Node randomCopNode = new Node(cur.random.val);
//复制链表中当前结点的random指针指向复制的结点
map.get(cur).random = randomCopNode;
//map中添加random结点的复制记录
map.put(cur.random, randomCopNode);
}else if(map.containsKey(cur.random)){
//如果random结点被复制过
//复制链表中当前结点的random指针指向map中的random结点
map.get(cur).random = map.get(cur.random);
}
//遍历下一个结点
cur = cur.next;
}
//返回复制的链表的头结点
return dummy.next;
}
}
方法二:哈希表+两次遍历
由于random随机指针的存在,我们在复制一个结点的时候,并没有办法直接去复制random指向的结点,因为我们并不知道这个random是否会指向我们前面已经复制过的结点,因此,我们需要一个HashMap去记录我们复制过的结点。
第一次遍历,填充map,键值对key-原链表结点--value-复制的链表结点(只复制了val值),第二次遍历,赋值next结点引用和random结点引用
如图所示,random域中的结点其实是中间原链表中的结点,为了方便理解,单独画出来了
我们通过map建立原链表与复制域结点的联系,原链表中的保存着random和next的引用
class Solution {
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
// 创建一个哈希表用于存储原节点和新节点之间的映射
Map<Node, Node> nodeMap = new HashMap<>();
// 第一次遍历:创建所有节点并存储在哈希表中
Node current = head;
while (current != null) {
nodeMap.put(current, new Node(current.val));
current = current.next;
}
// 第二次遍历:设置新节点的 next 和 random 指针
current = head;
while (current != null) {
nodeMap.get(current).next = nodeMap.get(current.next);
nodeMap.get(current).random = nodeMap.get(current.random);
current = current.next;
}
// 返回新的链表的头节点
return nodeMap.get(head);
}
}
一般也有可能会这样想,先不管random指针,直接复制整个链表的val, 每个结点的random先让它为null,然后因为要处理random,就需要通过复制的结点找到原来对应的结点,然后找到原来的random结点,最后又要通过该random结点找到复制的random结点。因此,我们还是比较容易想到要用个map来记录这种映射关系。而此时就需要多一次遍历了。前面复制链表是第一次遍历,第二次遍历,我们需要填充原节点到复制节点的映射以及复制节点到原节点的映射,第三次遍历,则是赋值复制链表的random指针,这里就不再写这个代码了。
方法三:哈希表+递归
由于random随机指针的存在,我们在复制一个结点的时候,并没有办法直接去复制random指向的结点,因为我们并不知道这个random是否会指向我们前面已经复制过的结点,因此,我们需要一个HashMap去记录我们复制过的结点。
class Solution {
Map<Node, Node> cachedNode = new HashMap<Node, Node>();
public Node copyRandomList(Node head) {
//当前子链表为空,直接返回
if (head == null) {
return null;
}
//如果当前子链表头结点没有复制过
if (!cachedNode.containsKey(head)) {
//复制头结点的值
Node headNew = new Node(head.val);
//保存复制记录
cachedNode.put(head, headNew);
//复制头结点后面的子链表,并拼接到复制的头结点后面
headNew.next = copyRandomList(head.next);
//复制以头结点random对应结点为头的子链表
//并使复制的头结点的random指向复制出来的子链表的头结点
headNew.random = copyRandomList(head.random);
}
//返回复制的头结点
return cachedNode.get(head);
}
}
headNew.random = copyRandomList(head.random);
这行代码改成下面这样,在题意中,因为传入的是一个完整的链表的头结点,也能通过。
if( head.random != null && !cachedNode.containsKey(head.random)){ Node randomNew = new Node(head.random.val); headNew.random = randomNew; cachedNode.put(head.random, randomNew); }else{ headNew.random = cachedNode.get(head.random); }但如果像这样,{val:1, random: 3}->{val:2, random:1}->{val:3, random:3}->{val:4, random:1}链表传入的head是{val:3, random:3},那么虽然3、4两个结点能正常复制,当前返回结果里,4号结点的random对应1号结点的next和random就没复制,同时,因为map是成员变量,如果第二次传入{val:1, random:3},那么会直接返回头结点(而且没有复制next和random),此时链表就没有成功复制。删除上面的if只留else也有问题,返回的4号结点的random直接就是null
聪明的你可能想到加个level入参,在第一层时就清空map, 就能解决上面第二个问题,但加粗的第一个问题却没有解决
class Solution { Map<Node, Node> cachedNode = new HashMap<Node, Node>(); public Node copyRandomList(Node head) { //第一层调用 return this.doCopyRandomList(head, 1); } public Node doCopyRandomList(Node head, int level) { if (head == null) { return null; } if(level == 1){ cachedNode.clear(); } if (!cachedNode.containsKey(head)) { Node headNew = new Node(head.val); cachedNode.put(head, headNew); //内层递归调用 headNew.next = doCopyRandomList(head.next, 2); if( head.random != null && !cachedNode.containsKey(head.random)){ Node randomNew = new Node(head.random.val); headNew.random = randomNew; cachedNode.put(head.random, randomNew); }else{ headNew.random = cachedNode.get(head.random); } } return cachedNode.get(head); } }针对重复调用需手动清空map的问题,可像这样完美解决:
class Solution { Map<Node, Node> cachedNode = new HashMap<Node, Node>(); public Node copyRandomList(Node head) { return this.doCopyRandomList(head); } public Node doCopyRandomList(Node head, int... level) { if (head == null) { return null; } if(level.length == 0){ cachedNode.clear(); } if (!cachedNode.containsKey(head)) { Node headNew = new Node(head.val); cachedNode.put(head, headNew); headNew.next = doCopyRandomList(head.next, 2); headNew.random = doCopyRandomList } return cachedNode.get(head); } }
方法四:三次遍历
我们知道原链表中有所有结点的random信息,因此我们可以将原链表当作缓存来用。原结点和复制结点的映射关系则由next来充当。
第一次遍历先复制各个结点的值,将复制结点接在原节点后面
第二次遍历赋值各个结点的random
第三次遍历处理各结点的next指向
class Solution {
public Node copyRandomList(Node head) {
//链表为空,直接返回
if(head == null){
return head;
}
//第一次遍历,复制结点的val, 原结点-复制品-原结点-复制品
for(Node cur = head; cur != null; cur = cur.next.next){
Node curNew = new Node(cur.val);
curNew.next = cur.next;
cur.next = curNew;
}
//第二次遍历,给各个复制结点的random赋值
for(Node cur = head; cur != null; cur = cur.next.next){
cur.next.random = cur.random == null ? null : cur.random.next;
}
//第三次遍历,采用双指针拆分两个链表(拆分奇偶链表)
Node headNew = head.next;
Node p = head, q = head.next, ppre = head, qpre = head.next;
for(; q != null && q.next != null;){
q = q.next.next;
p = p.next.next;
ppre.next = p;
qpre.next = q;
ppre = p;
qpre = q;
}
if(q == null){
qpre.next = null;
}
if(q.next == null){
p.next = null;
}
return headNew;
}
}
关于第三次遍历,其实就是拆分奇偶链表,LeetCode328。
上面采用的是双指针+维护pre
当然,遍历+维护tail也是可以的
class Solution { public Node copyRandomList(Node head) { //链表为空,直接返回 if(head == null){ return head; } //第一次遍历,复制结点的val, 原结点-复制品-原结点-复制品 for(Node cur = head; cur != null; cur = cur.next.next){ Node curNew = new Node(cur.val); curNew.next = cur.next; cur.next = curNew; } //第二次遍历,给各个复制结点的random赋值 for(Node cur = head; cur != null; cur = cur.next.next){ cur.next.random = cur.random == null ? null : cur.random.next; } //第三次遍历,拆分两个链表(拆分奇偶链表) Node headNew = head.next; Node dummy = new Node(-1); Node dummyNew = new Node(-1); Node tail = dummy, tailNew = dummyNew; int count = 1; for(Node cur = head; cur != null; cur = cur.next){ if(count % 2 == 1){ tail.next = cur; tail = tail.next; }else{ tailNew.next = cur; tailNew = tailNew.next; } count++; } tail.next = null; tailNew.next = null; return headNew; } }








762

被折叠的 条评论
为什么被折叠?



