Question

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2↘c1 → c2 → c3↗
B:     b1 → b2 → b3

begin to intersect at node c1.

Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

Solution

Key to the solution here is to traverse two lists to get their lengths. Therefore, we can move the pointer for the longer list first and then compare elements of both lists.

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14         if (headA == null || headB == null)
15             return null;
16         ListNode p1 = headA, p2 = headB;
17         int l1 = 0, l2 = 0, ll = 0;
18         while (p1 != null) {
19             l1++;
20             p1 = p1.next;
21         }
22         while (p2 != null) {
23             l2++;
24             p2 = p2.next;
25         }
26         p1 = headA;
27         p2 = headB;
28         if (l2 >= l1) {
29             ll = l2 - l1;
30             while (ll > 0) {
31                 p2 = p2.next;
32                 ll--;
33             }
34         } else {
35             ll = l1 - l2;
36             while (ll > 0) {
37                 p1 = p1.next;
38                 ll--;
39             }
40         }
41         while (p1 != null && p2 != null) {
42             if (p1.val == p2.val)
43                 return p1;
44             p1 = p1.next;
45             p2 = p2.next;
46         }
47         return null;
48     }
49 }

转载于:https://www.cnblogs.com/ireneyanglan/p/4812526.html

Intersection of Two Linked Lists 解答相关推荐

  1. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. LeetCode(160): Intersection of Two Linked Lists

    Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...

  3. Intersection of Two Linked Lists——经典问题

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  5. LeetCode Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. 【leetcode】Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. [LeetCode]--160. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. 【LeetCode从零单排】No.160 Intersection of Two Linked Lists

    题目 Write a program to find the node at which the intersection of two singly linked lists begins. For ...

最新文章

  1. (转载博文)VC++API速查
  2. python返回unicode_Python 中通过 unicode 码返回单字符的函数是 ,返回单字符 unicode 码的函数是 。_学小易找答案...
  3. 存储时间:从Symmetrix V-Max看高端存储的未来
  4. python3 实现 websocket server 解决中文乱码
  5. 强烈推荐一位大佬,知名银行风控分析师,学习是一辈子的事!
  6. [BUUCTF-pwn]——picoctf_2018_buffer overflow 0
  7. 联想gen系列服务器,Hpe Microserver Gen10 Plus开箱
  8. SpringData环境搭建代码编写
  9. 11001-软件架构设计风格及visio使用
  10. Java八大算法:归并排序
  11. 使用docker搭建steam 饥荒服务器
  12. 庄懂技术美术入门课笔记_L13_特效类shader(AlphaBlendAlphaCutoutAdditice)
  13. 解决后台传入的大于js最大数值精度的问题
  14. mini计算机结构,简单拆机看内部构造_苹果 Mac mini MGEN2CH/A_台式电脑评测-中关村在线...
  15. 华为员工工资曝光:入职12年月薪31万!手里的窝窝头突然就不香了.....
  16. 2022春招第一波投递时间预测,早看早知道
  17. onvif 修改摄像头参数
  18. android studio seekbar 简单音乐播放器
  19. 条码打印出现乱码的解决方案
  20. 数据结构与算法--第二章pro题解

热门文章

  1. 《LeetCode力扣练习》第136题 只出现一次的数字 Java
  2. centos7安装Oracle12(完整版)
  3. C# DataGridView控件用法
  4. tendermint+java_tendermint简介
  5. selenium 验证码识别_如何获取验证码?
  6. 绝地求生 android版支持蓝牙吗,绝地求生怎么蓝牙耳机设置听脚步 | 手游网游页游攻略大全...
  7. c语言的十进制转十六进制字符串,用c语言写一个函数把十进制转换成十六进制,该如何处理...
  8. 订阅者java_RxJava:“java.lang.IllegalStateException:只允许一个订阅者!”
  9. ArcGIS Engine10.4版本
  10. 12、动态视图组件ListView、GridView