寻找链表相交节点
2017-04-11 10:43
113 查看
思路一:
1、先求出两个链表ListA,ListB的长度LengthA,LengthB。
2、然后先把长的链表头指针先往前走|LengthA - LengthB|步。
3、最后两个链表头指针同步往前走一步,直到指向的对象相同。
代码实现及测试用例:
思路2:
不用求出链表长度的值,但其实算法复杂度是一样的。
We can use two iterations to do that.
In the first iteration, we will reset the pointer of one linkedlist to the head of another linkedlist after it reaches the tail node.
In the second iteration, we will move two pointers until they points to the same node.
Our operations in first iteration will help us counteract the difference.
So if two linkedlist intersects, the meeting point in second iteration must be the intersection point.
If the two linked lists have no intersection at all, then the meeting pointer in second iteration must be the tail node of both lists, which is null
代码实现:
参考:https://leetcode.com/problems/intersection-of-two-linked-lists/#/solutions
1、先求出两个链表ListA,ListB的长度LengthA,LengthB。
2、然后先把长的链表头指针先往前走|LengthA - LengthB|步。
3、最后两个链表头指针同步往前走一步,直到指向的对象相同。
代码实现及测试用例:
package com.qiusongde; public class Solution160 { public static void main(String[] args) { ListNode l1 = new ListNode(1); ListNode l2 = new ListNode(2); ListNode l3 = new ListNode(3); l1.next = l2; l2.next = l3; ListNode l4 = new ListNode(4); ListNode l5 = new ListNode(5); l4.next = l5; l5.next = l3; showListNode(l1); showListNode(l4); System.out.println(getIntersectionNode(l1, l4).val); } public static ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA == null || headB == null) { return null; } //compute the length of two list int sizeA = length(headA); int sizeB = length(headB); //counteract difference while(sizeA > sizeB) { headA = headA.next; sizeA--; } while(sizeB > sizeA) { headB = headB.next; sizeB--; } //find the same node //if no intersection, the same node is null while(headA != headB) { headA = headA.next; headB = headB.next; } return headA; } private static int length(ListNode node) { int length = 0; while(node != null) { length++; node = node.next; } return length; } public static void showListNode(ListNode list) { if(list == null) { System.out.println(list); return; } while(list != null) { System.out.print(list.val + " "); list = list.next; } System.out.println(); } public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } }
1 2 3 4 5 3 3
思路2:
不用求出链表长度的值,但其实算法复杂度是一样的。
We can use two iterations to do that.
In the first iteration, we will reset the pointer of one linkedlist to the head of another linkedlist after it reaches the tail node.
In the second iteration, we will move two pointers until they points to the same node.
Our operations in first iteration will help us counteract the difference.
So if two linkedlist intersects, the meeting point in second iteration must be the intersection point.
If the two linked lists have no intersection at all, then the meeting pointer in second iteration must be the tail node of both lists, which is null
代码实现:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { //boundary check if(headA == null || headB == null) return null; ListNode a = headA; ListNode b = headB; //if a & b have different len, then we will stop the loop after second iteration while( a != b){ //for the end of first iteration, we just reset the pointer to the head of another linkedlist a = a == null? headB : a.next; b = b == null? headA : b.next; } return a; }
参考:https://leetcode.com/problems/intersection-of-two-linked-lists/#/solutions
相关文章推荐
- 数据结构—判断两个链表是否相交,寻找两个链表的相交节点
- 判断两个单链表是否相交同时找出第一个相交点(单链表是否有环及寻找入口节点)
- 寻找两个相交链表的第一个公共节点
- 寻找两个相交的单链表的第一个节点
- 寻找两个相交链表的第一个公共节点
- 寻找两个相交链表的第一个公共节点的问题
- 链表节点链表相交(及相交的节点)、判断链表是否有环问题
- 判断链表是否有环,找链表中间节点,判断两个链表是否相交
- Cracking the Coding Interview:: 寻找有环链表的环路起始节点
- 数据结构之链表面试题汇总(四)得到两个单链表相交的第一个交点、用O(1)的时间效率删除单向链表中的指定节点
- 寻找链表的倒数第k个节点
- 寻找链表的中间节点
- 单向链表相交的第一个公共结点, 判断链表是否有环以及环的入口节点
- 单链表删除头节点和不知道N的情况下寻找中间节点
- CCI 2.6 寻找有环链表环路的开头节点
- 两个链表相交,要求找出第一个相交的节点
- 判断链表相交,删除无头单链表的非尾节点及倒序打印单链表——题集(四)
- 寻找链表的中间节点
- java链表寻找中间节点