阅读该文章预计30分钟
一:复制含有随机指针节点的链表
1.1:题目描述
【题目】 一种特殊的链表节点类描述如下:
public static class Node {
private int value;
private Node next;
private Node rand;
public Node(int value) {
this.value = value;
}
}
Node类中的value是节点值,next指针和正常单链表中next指针的意义 一 样,都指向下一个节点,rand指针是Node类中新增的指针,这个指 针可 能指向链表中的任意一个节点,也可能指向null。 给定一个由 Node节点类型组成的无环单链表的头节点head,请实现一个 函数完成 这个链表中所有结构的复制,并返回复制的新链表的头节点。
进阶: 不使用额外的数据结构,只用有限几个变量,且在时间复杂度为 O(N) 内完成原问题要实现的函数。
1.2:思路分析
1.2.1:空间复杂度O(N)
使用map把所有结点储存起来,只保存他的值,不保存他的引用
然后遍历头节点
给每个节点的引用(next、rand)指针赋值
复制的节点从map中拿出,组装next、rand指针,因为保存的是他的引用,所以用map刚好
1.2.2:空间复杂度O(1)
将所有节点都复制一份,复制到他的下面
如这样 A-B-C 复制后:A-A-B-B-C-C
然后再给第二个A、B、C复制next、rand指针
next指针就是原来next指针的下一个
rand指针就是原来rand指针的下一个
1.3:Java代码
package xyz.fudongyang.basic.class_03.my;
import java.util.HashMap;
import java.util.Map;
public class Code_13_CopyListWithRandom {
public static class Node {
private int value;
private Node next;
private Node rand;
public Node(int value) {
this.value = value;
}
}
public static Node copyListWithRand1(Node head) {
if (head == null) {
return null;
}
Map<Node, Node> dataMap = new HashMap<>();
Node cur = head;
while (cur != null){
dataMap.put(cur,new Node(cur.value));
cur = cur.next;
}
cur = head;
while (cur != null){
dataMap.get(cur).next = dataMap.get(cur.next);
dataMap.get(cur).rand = dataMap.get(cur.rand);
cur = cur.next;
}
return dataMap.get(head);
}
public static Node copyListWithRand2(Node head) {
if (head == null){
return null;
}
Node cur = head;
Node next = null;
while (cur != null){
next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
Node curCopy = null;
while (cur != null){
next = cur.next.next;
curCopy = cur.next;
curCopy.rand = cur.rand == null ? null : cur.rand.next;
cur = next;
}
Node res = head.next;
cur = head;
while (cur != null){
next = cur.next.next;
curCopy= cur.next;
cur.next = next;
curCopy.next = next != null ? next.next : null;
cur = next;
}
return res;
}
public static void printRandLinkedList(Node head) {
Node cur = head;
System.out.print("order: ");
while (cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.println();
cur = head;
System.out.print("rand: ");
while (cur != null) {
System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
Node res1 = null;
Node res2 = null;
printRandLinkedList(head);
res1 = copyListWithRand1(head);
printRandLinkedList(res1);
res2 = copyListWithRand2(head);
printRandLinkedList(res2);
printRandLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.rand = head.next.next.next.next.next;
head.next.rand = head.next.next.next.next.next;
head.next.next.rand = head.next.next.next.next;
head.next.next.next.rand = head.next.next;
head.next.next.next.next.rand = null;
head.next.next.next.next.next.rand = head.next.next.next;
printRandLinkedList(head);
res1 = copyListWithRand1(head);
printRandLinkedList(res1);
res2 = copyListWithRand2(head);
printRandLinkedList(res2);
printRandLinkedList(head);
System.out.println("=========================");
}
}
二:两个单链表相交的节点
2.1:题目描述
【题目】 在本题中,单链表可能有环,也可能无环。给定两个 单链表的头节点 head1和head2,这两个链表可能相交,也可能 不相交。请实现一个函数, 如果两个链表相交,请返回相交的 第一个节点;如果不相交,返回null 即可。
要求:如果链表1 的长度为N,链表2的长度为M,时间复杂度请达到 O(N+M),额外 空间复杂度请达到O(1)。
2.2:题目分析
两个单链表A、B,这里的相交指的是内存地址相同
@1两个单链表都无环 可能相交、可能不相交
@2两个单链表一个有环一个无环 不可能相交
@3两个单链表有环 可能相交可能不相交
所以先判断两个链表有环不
@1情况:
如果两个都无环,如果相交,他们相交后的节点是相同的,所以相交后的个数也是相同的
所以我们可以分为 长链表、短链表
让长链表先走比较长的一部分,待两个链表长度都一样,再逐一比较,直到为空
@2情况:
直接返回null、不可能相交
@3情况:
如果两个有环单链表相交分为两种情况
1)入环节点相同 像数字“6 6”这个样子
2)入环节点不同 像6/这个样子,单斜杠和6共用下面那个圈
如果是1)我们只需要像@1一样,分为长短链表,只不过是到最后一个节点结束,
因为有环,不能到达最后一个节点,只能到入环节点那个位置
如果是2)只需要两个入环节点遍历即可
2.3:Java代码
package xyz.fudongyang.basic.class_03.my;
public class Code_14_FindFirstIntersectNode1 {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head2 == null){
return null;
}
Node head1LoopNode = getLoopNode(head1);
Node head2LoopNode = getLoopNode(head2);
if (head1LoopNode == null && head2LoopNode == null){
return noLoop(head1,head2);
}
if (head1LoopNode != null && head2LoopNode != null){
return bothLoop(head1,head1LoopNode,head2,head2LoopNode);
}
return null;
}
public static Node getLoopNode(Node head) {
if (head == null || head.next == null || head.next.next == null){
return null;
}
Node cur1 = head;
Node cur2 = head;
do {
if (cur2.next == null || cur2.next.next == null){
return null;
}
cur1 = cur1.next;
cur2 = cur2.next.next;
}while (cur1 != cur2);
cur2 = head;
while (cur1 != cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null){
return null;
}
int n = 0;
Node cur1 = head1;
Node cur2 = head2;
while (cur1 != null){
n++;
cur1 = cur1.next;
}
while (cur2 != null){
n--;
cur2 = cur2.next;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n>0){
n--;
cur1 = cur1.next;
}
while (cur1 != null){
if (cur1 == cur2){
return cur1;
}
cur1 = cur1.next;
cur2 = cur2.next;
}
return null;
}
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
if (loop1 == loop2){
int n = 0;
Node cur1 = head1;
Node cur2 = head2;
while (cur1 != loop1){
n++;
cur1 = cur1.next;
}
while (cur2 != loop2){
n--;
cur2 = cur2.next;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n>0){
n--;
cur1 = cur1.next;
}
while (cur1 != cur2){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}else {
Node cur1 = loop1.next;
while (cur1 != loop1){
if (cur1 == loop2){
return loop1;
}
cur1 = cur1.next;
}
}
return null;
}
public static void main(String[] args) {
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
Node head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next;
System.out.println(getIntersectNode(head1, head2).value);
head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
head1.next.next.next.next.next.next = head1.next.next.next;
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next;
System.out.println(getIntersectNode(head1, head2).value);
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next;
System.out.println(getIntersectNode(head1, head2).value);
}
}
|