1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * public int val;
  5. * public ListNode next;
  6. * public ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
  11. {
  12. if (headA == null || headB == null)
  13. {
  14. return null;
  15. }
  16. else
  17. {
  18. var tempA = headA;
  19. var tempB = headB;
  20.  
  21. var listA = new List<ListNode>();
  22. var listB = new List<ListNode>();
  23. while (headA != null)
  24. {
  25. listA.Add(headA);
  26. headA = headA.next;
  27. }
  28. while (headB != null)
  29. {
  30. listB.Add(headB);
  31. headB = headB.next;
  32. }
  33.  
  34. listA.Reverse();
  35. listB.Reverse();
  36.  
  37. if (listA.Count > listB.Count)
  38. {
  39. for (int i = ; i < listB.Count; i++)
  40. {
  41. if (listA[i].val != listB[i].val)
  42. {
  43. if (i > )
  44. {
  45. tempB = listB[i - ];
  46. }
  47. else
  48. {
  49. tempB = null;
  50. }
  51. break;
  52. }
  53. else
  54. {
  55. if (i > )
  56. {
  57. tempB = listB[i];
  58. }
  59. }
  60. }
  61. return tempB;
  62. }
  63. else
  64. {
  65. for (int i = ; i < listA.Count; i++)
  66. {
  67. if (listA[i].val != listB[i].val)
  68. {
  69. if (i > )
  70. {
  71. tempA = listA[i - ];
  72. }
  73. else
  74. {
  75. tempA = null;
  76. }
  77. break;
  78. }
  79. else
  80. {
  81. if (i > )
  82. {
  83. tempA = listA[i];
  84. }
  85. }
  86. }
  87. return tempA;
  88. }
  89. }
  90. }
  91. }

https://leetcode.com/problems/intersection-of-two-linked-lists/#/description

补充一个python的实现:

  1. class Solution(object):
  2. def getIntersectionNode(self, headA, headB):
  3. """
  4. :type head1, head1: ListNode
  5. :rtype: ListNode
  6. """
  7. tempA = headA
  8. tempB = headB
  9. while tempA != tempB:
  10. if tempA == None:
  11. tempA = headB
  12. else:
  13. tempA = tempA.next
  14. if tempB == None:
  15. tempB = headA
  16. else:
  17. tempB = tempB.next
  18. return tempA

leetcode160的更多相关文章

  1. [LeetCode160]Intersection of Two Linked Lists

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

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

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

  3. LeetCode160.相交链表

    编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...

  4. leetcode-160周赛-5241-铺瓷砖

    题目描述: 方法一:动态规划 class Solution: def f(self, n, m): if n < m: n, m = m, n if (n, m) in self.mem: re ...

  5. leetcode-160周赛-5240-串联字符串的最大长度

    题目描述: 自己的提交:O(2**n∗n∗m),m 为字符串长度 class Solution: def maxLength(self, arr: List[str]) -> int: from ...

  6. leetcode-160周赛-5239-循环码排列

    题目描述: 参考格雷编码: class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: res ...

  7. leetcode-160场周赛-5238-找出给定方程的正整数解

    题目描述: class Solution: def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[Li ...

  8. LeetCode160 相交链表(双指针)

    题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...

  9. 朋友遇到过的t厂面试题

    朋友遇到过的t面试题 leetcode160 找链表交点 leetcode206 反转链表

随机推荐

  1. ubuntu 16.04 编译安装 trl8291cu系列 无线网卡驱动

    1 先 下载git包 和相关编译工具 sudo apt-get update sudo apt-get install git linux-headers-generic build-essentia ...

  2. Problem A: 类的初体验

    Description 定义一个类Data,只有一个double类型的属性和如下3个方法: 1.    void init(double d);——初始化属性值. 2.   double getVal ...

  3. 说说VBA中的面向对象

    对象是 Visual Basic 的结构基础,在 Visual Basic 中进行的所有操作几乎都与修改对象有关.Microsoft Word 的任何元素,如文档.表格.段落.书签.域等,都可用 Vi ...

  4. 团队项目(MVP------新能源无线充电管理网站)(总结)

    经过了几个月的学习时间与团队的磨合以及一系列的困难之后,我们mvp小组一起完成了这个项目,内心也是十分激动和有成就感的.其实一开始基础并不好,很多都不知道,但是通过在慕课网上的学习以及老师严厉地督促下 ...

  5. 外网访问SQLServer数据库holer实现

    外网访问内网SQLServer数据库 内网主机上安装了SQLServer数据库,只能在局域网内访问,怎样从公网也能访问本地SQLServer数据库? 本文将介绍使用holer实现的具体步骤. 1. 准 ...

  6. matrix_chain_order

    to calculate the min step of multiplicate some matixs package dynamic_programming; public class matr ...

  7. HTML和CSS标签常用命名规则

    1.Images 存放一些网站常用的图片: 2.Css 存放一些CSS文件: 3.Flash 存放一些Flash文件: 4.PSD 存放一些PSD源文件: 5.Temp 存放所有临时图片和其它文件: ...

  8. PS 给照片换背景

    1. 打开一张照片,导入证件照 2. 点击选择 => 选择并遮住 (快捷键 command + option + r) 3. 点击快速选择工具,将属性设置里面的视图模式选择为洋葱皮,鼠标点击需要 ...

  9. redis 的备份策略,最好使用:RDB-AOF 混合持久化

    相关资料: Redis 4.0 新功能简介:RDB-AOF 混合持久化:http://blog.huangz.me/2017/redis-rdb-aof-mixed-persistence.html ...

  10. [转]Windows下使用VS2015编译openssl库

    转自:http://blog.csdn.net/alger_magic/article/details/52584171 目标:编译vs环境下openssl库 工具: 1. 编译环境win10+vs2 ...