注:此博客不再更新,所有最新文章将发表在个人独立博客limengting.site。分享技术,记录生活,欢迎大家关注
题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
方法一:使用HashMap,空间复杂度O(n)
/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
import java.util.HashMap;
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
// HashMap,O(n)
if (pHead == null) return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
RandomListNode cur = pHead;
// copy
while (cur != null) {
map.put(cur, new RandomListNode(cur.label));
cur = cur.next;
}
cur = pHead;
// set next and random
while (cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
return map.get(pHead);
}
}
方法二:链表中直接复制在该节点后面,再拆成两条链表,空间复杂度O(1)
/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
import java.util.HashMap;
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
if (pHead == null) return null;
RandomListNode cur = pHead;
RandomListNode next = null;
// copy value
while (cur != null) {
next = cur.next; // store
cur.next = new RandomListNode(cur.label);
cur.next.next = next;
cur = next;
}
cur = pHead;
RandomListNode curCopy = null;
// set random
while (cur != null) {
curCopy = cur.next;
curCopy.random = cur.random == null ? null : cur.random.next;
cur = cur.next.next;
}
RandomListNode res = pHead.next;
cur = pHead;
// split
while (cur != null) {
next = cur.next.next;
curCopy = cur.next;
cur.next = next;
curCopy.next = next == null ? null : next.next;
cur = cur.next;
}
return res;
}
}
汇总调试程序:
package sword_to_offer_linkedlist;
/*输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点)
返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)*/
import java.util.HashMap;
public class Solution {
public static class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
public static RandomListNode Clone1(RandomListNode pHead) {
// HashMap,O(n)
if (pHead == null) return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
RandomListNode cur = pHead;
// copy
while (cur != null) {
map.put(cur, new RandomListNode(cur.label));
cur = cur.next;
}
cur = pHead;
// set next and random
while (cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
return map.get(pHead);
}
public static RandomListNode Clone(RandomListNode pHead) {
if (pHead == null) return null;
RandomListNode cur = pHead;
RandomListNode next = null;
// copy value
while (cur != null) {
next = cur.next; // store
cur.next = new RandomListNode(cur.label);
cur.next.next = next;
cur = next;
}
cur = pHead;
RandomListNode curCopy = null;
// set random
while (cur != null) {
curCopy = cur.next;
curCopy.random = cur.random == null ? null : cur.random.next;
cur = cur.next.next;
}
RandomListNode res = pHead.next;
cur = pHead;
// split
while (cur != null) {
next = cur.next.next;
curCopy = cur.next;
cur.next = next;
curCopy.next = next == null ? null : next.next;
cur = cur.next;
}
return res;
}
public static void printRandLinkedList(RandomListNode head) {
RandomListNode cur = head;
System.out.print("order: ");
while (cur != null) {
System.out.print(cur.label + " ");
cur = cur.next;
}
System.out.println();
cur = head;
System.out.print("random: ");
while (cur != null) {
System.out.print(cur.random == null ? "- " : cur.random.label + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
RandomListNode head = new RandomListNode(1);
head.next = new RandomListNode(2);
head.next.next = new RandomListNode(3);
head.next.next.next = new RandomListNode(4);
head.next.next.next.next = new RandomListNode(5);
head.next.next.next.next.next = new RandomListNode(6);
head.random = head.next.next.next.next.next; // 1 -> 6
head.next.random = head.next.next.next.next.next; // 2 -> 6
head.next.next.random = head.next.next.next.next; // 3 -> 5
head.next.next.next.random = head.next.next; // 4 -> 3
head.next.next.next.next.random = null; // 5 -> null
head.next.next.next.next.next.random = head.next.next.next; // 6 -> 4
System.out.println("Original LinkedList:");
printRandLinkedList(head);
System.out.println("Copy LinkedList:");
RandomListNode res1 = Clone(head);
printRandLinkedList(res1);
System.out.println("Original LinkedList After Copy");
printRandLinkedList(head);
System.out.println("=============================");
System.out.println("Original LinkedList:");
printRandLinkedList(head);
RandomListNode res2 = Clone(head);
System.out.println("Copy LinkedList:");
printRandLinkedList(res2);
System.out.println("Original LinkedList After Copy");
printRandLinkedList(head);
}
}