博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
寻找链表相交节点
阅读量:5829 次
发布时间:2019-06-18

本文共 3250 字,大约阅读时间需要 10 分钟。

思路一:

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://www.cnblogs.com/songdechiu/p/6692470.html

你可能感兴趣的文章
Breaking parallel loops in .NET C# using the Stop method z
查看>>
修改故障转移群集心跳时间
查看>>
[轉]redis;mongodb;memcache三者的性能比較
查看>>
微软职位内部推荐-Sr DEV
查看>>
让你的WPF程序在Win7下呈现Win8风格主题
查看>>
802.11 学习笔记
查看>>
Leetcode-Database-176-Second Highest Salary-Easy(转)
查看>>
构建Docker Compose服务堆栈
查看>>
最小角回归 LARS算法包的用法以及模型参数的选择(R语言 )
查看>>
Hadoop生态圈-Kafka常用命令总结
查看>>
如何基于Redis Replication设计并实现Redis-replicator?
查看>>
浮点数内存如何存储的
查看>>
贪吃蛇
查看>>
EventSystem
查看>>
用WINSOCK API实现同步非阻塞方式的网络通讯
查看>>
玩一玩博客,嘿嘿
查看>>
P1352 没有上司的舞会
查看>>
ios11文件夹
查看>>
【HLOJ 559】好朋友的题
查看>>
Electric Fence(皮克定理)
查看>>