Total Accepted: 53721 Total Submissions: 180705 Difficulty: Easy

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.
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:int getListLength(ListNode* head){int len = 0;while(head){len++;head=head->next;}return len;}ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {int lenA = getListLength(headA);int lenB = getListLength(headB);ListNode* common = NULL,*startA=headA,*startB=headB;if(lenA<lenB){int diff = lenB-lenA;while(diff--) startB=startB->next;}else{int diff = lenA-lenB;while(diff--) startA=startA->next;}while(startA){if(startA == startB){common = startA;break;}startA=startA->next;startB=startB->next;}return common;}
};

Next challenges: (M) Swap Nodes in Pairs (H) Copy List with Random Pointer (M) Insertion Sort List

转载于:https://www.cnblogs.com/zengzy/p/5041278.html

[Linked List]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. Intersection of Two Linked Lists 解答

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

  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】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. [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 ...

  9. Intersection of Two Linked Lists

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

  10. 【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. LeetCode 804 Unique Morse Code Words--python,java解法
  2. FD.io/VPP — L2TP
  3. php删除数据库中数据表的数据,php中删除数据库数据例子
  4. hadoop集群swap_性能瓶颈--MEM(swap)
  5. 转:实例学习PHP程序对用户身份认证实现两种方法
  6. 关于nginx/lighttpd epoll高并发以及apache为何不采用epoll的的疑惑 不指定
  7. 如何将Webpack与React结合使用:深入的教程
  8. 2.SOA架构:服务和微服务分析及设计--- 理解面向服务架构
  9. PHP倒序后五个字符串,并显示
  10. SHELL下把一个文件附加到另外一个文件,注意编码问题
  11. Valve员工手册,一部震惊世界的“人生攻略”
  12. 万能的MATLAB丨大厂工程师必备技能,免费学习
  13. python制作软件安装包_Python安装包及开发工具
  14. 人行征信报告(上)——一代征信报告的变量梳理
  15. Windows 10 操作系统,更新之后无法打开Windows Mobile 设备中心,要么双击无反应。要么正在启动,过会就关闭了。
  16. 怎样删除usb计算机连接网络打印机驱动,打印机USB连接安装设置及常见问题处理...
  17. c语言tc2.0编译器下载,c语言编译器|c语言编译器(wintc)_绿茶手机网
  18. 计算机基础 华师在线,华师在线计算机基础试题答案.doc
  19. MacBook重启之后,外接显示器不亮的解决方法
  20. python特效源代码_人工智能python代码实现魔幻换天视频特效

热门文章

  1. 经常会用到的ocr文字识别工具:Text Scanner for Mac
  2. 使用Screaming Frog SEO Spider 如何查找断开的链接
  3. 通信原理实践(五)——2PSK 与2DPSK 通信系统
  4. SQL数据库学习之路(八)
  5. 微信实现双向跨境支付,将向香港用户开放内地支付服务
  6. 漏洞扫描与云服务器的安全防护
  7. JavaScript中的[]和{}
  8. beanstalkd最佳实践-编程开发
  9. 《***大曝光》作者:怎样造就***高手
  10. 2008秋季-计算机软件基础-0922课堂用例(2)