【题目】
有一种特殊的链表节点类,如下所示
class Node{
int value;
Node next;
Node rand;
Node(int val){
value = val;
}
}
rand指针是单链表节点结构中新增的指针,rand可能指向链表中的任意一个结点,或者是null,
给定一个由Node节点类型组成的无环单链表的头节点head,
请实现一个函数,完成这个链表的复制,并返回复制的新链表的头节点
【要求】时间复杂度是O(N),额外空间复杂度是O(1)
【思路】
有2种方式:
- 利用hashmap,用原链表节点作key,value为新建节点,复制相应的next节点和rand节点
- 在原链表的基础上复制链表,最后分离开来
具体代码实现如下
hashmap
private static Node copyListWithRandom1(Node head){
HashMap<Node, Node> map = new HashMap<>();
Node cur = head;
while (null != cur){
map.put(cur,new Node(cur.value));
cur = cur.next;
}
cur = head;
while (null != cur){
map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand);
cur = cur.next;
}
return map.get(head);
}
复制链表
private static Node copyListWithRandom2(Node head){
Node cur = head;
Node next;
//复制节点
while (null != cur){
next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
while (null != cur){
if (null != cur.rand){
cur.next.rand = cur.rand.next;
}
cur = cur.next.next;
}
cur = head;
Node res = head.next;
Node copyNode;
//分离
while (null != cur){
next = cur.next.next;
copyNode = cur.next;
cur.next = next;
copyNode.next = null != next ? next.next : null;
cur = next;
}
return res;
}