数据结构与算法之复制含有随机指针节点的链表和两个链表相交的一系列问题
生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法之复制含有随机指针节点的链表和两个链表相交的一系列问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構與算法復制含有隨機指針節點的鏈表和兩個鏈表相交的一系列問題
目錄
1. 復制含有隨機指針節點的鏈表
題目概述
思路:創建一個HashMap結構,key放當前Node,value放需要復制的Node。
代碼實現
2. 兩個鏈表相交的一系列問題
1. 題目描述
2. 思路
得到入環節點:
無環鏈表相交問題
有環鏈表相交問題
創建兩個變量cur1,cur2.
如果兩個入環節點相同,則將head1賦給cur1,head2賦給cur2.創建一個變量n記錄head1和head2的長度。
遍歷head1,每走一步n++,遍歷head2,每走一步n–。最后得到的n值即為兩個鏈表的長度差。
復用cur1和cur2,當n>0時,說明head1比較長,將cur1指向head1,否則head2比較長,將cur1,指向head2。cur2指向短的鏈表head1或head2。將n取絕對值。
cur1先走n步,跟cur2一樣長時,兩者一起走,當兩者值一樣時即為無環鏈表相交點。返回cur1即可。
如果兩個入環節點不同,則可能相交也可能不相交。
使cur1 = loop1.next,遍歷環結構,如果途中遇到loop2,即為相交,否則不相交。
3. 代碼實現
public class Code_FindFirstIntersectNode {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 loop1 = getLoopNode(head1);Node loop2 = getLoopNode(head2);if (loop1 == null && loop2 == null) {return noLoop(head1, head2);}if (loop1 != null && loop2 != null) {return bothLoop(head1, loop1, head2, loop2);}return null;}//得到入環節點public static Node getLoopNode(Node head) {if (head == null || head.next == null || head.next.next == null) {return null;}Node n1 = head.next; // n1 -> slowNode n2 = head.next.next; // n2 -> fastwhile (n1 != n2) {if (n2.next == null || n2.next.next == null) {return null;}n2 = n2.next.next;n1 = n1.next;}n2 = head; // n2 -> walk again from headwhile (n1 != n2) {n1 = n1.next;n2 = n2.next;}return n1;}//無環鏈表相交問題public static Node noLoop(Node head1, Node head2) {if (head1 == null || head2 == null) {return null;}Node cur1 = head1;Node cur2 = head2;int n = 0;while (cur1.next != null) {n++;cur1 = cur1.next;}while (cur2.next != null) {n--;cur2 = cur2.next;}if (cur1 != cur2) {return null;}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;}// 兩個有環鏈表相交問題public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {Node cur1 = null;Node cur2 = null;if (loop1 == loop2) {cur1 = head1;cur2 = head2;int n = 0;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 {cur1 = loop1.next;while (cur1 != loop1) {if (cur1 == loop2) {return loop1;}cur1 = cur1.next;}return null;}}public static void main(String[] args) {// 1->2->3->4->5->6->7->nullNode 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);// 0->9->8->6->7->nullNode 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; // 8->6System.out.println(getIntersectNode(head1, head2).value);// 1->2->3->4->5->6->7->4...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; // 7->4// 0->9->8->2...head2 = new Node(0);head2.next = new Node(9);head2.next.next = new Node(8);head2.next.next.next = head1.next; // 8->2System.out.println(getIntersectNode(head1, head2).value);// 0->9->8->6->4->5->6..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; // 8->6System.out.println(getIntersectNode(head1, head2).value);}}總結
以上是生活随笔為你收集整理的数据结构与算法之复制含有随机指针节点的链表和两个链表相交的一系列问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法之打印两个有序链表公共部分
- 下一篇: 数据结构与算法之二叉树的先序遍历,中序遍