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.

解题思路:

先遍历两个list的长度,然后长的减去短的,之后同时遍历即可,JAVA实现如下:

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null)
return null;
int lengthA=0,lengthB=0;
ListNode tempA=headA,tempB=headB;
while(tempA!=null){
tempA=tempA.next;
lengthA++;
}
while(tempB!=null){
tempB=tempB.next;
lengthB++;
}
if(lengthB>lengthA){
tempA=headA;
headA=headB;
headB=tempA;
int temp=lengthA;
lengthA=lengthB;
lengthB=temp;
}
int length=lengthA-lengthB;
while(length-->0)
headA=headA.next;
while(headA!=null){
if(headA==headB)
return headA;
headA=headA.next;
headB=headB.next;
}
return headA;
}

最新文章

  1. querySelector系列方法相比 getElementsBy 系列方法有什么区别?
  2. android帧动画,移动位置,缩放,改变透明度等动画讲解
  3. signalr遇到的问题汇总
  4. java 图片处理工具类
  5. Mvc网站发布到IIS
  6. 前端编码规范(4)—— CSS 和 Sass (SCSS) 规范
  7. [backbone]backbone.js
  8. hdu 4409 LCA
  9. Spring之Spring MVC
  10. 今天遇到的i++问题之记录
  11. Jtree(节点的渲染+资源管理器)(2)
  12. Delphi 的动态数组
  13. Python - 定制pattern的string模板(template) 具体解释
  14. BFS和DFS详解
  15. Python第一天接触心得
  16. 二:Maven中pom.xml元素详解
  17. MySQL复制相关变量
  18. java基础(五)-----关键字static
  19. 《Blue_Flke》 团队项目用户验收评审
  20. 【Python】解决测试依赖之 Mock模块的基本使用

热门文章

  1. easyVS
  2. Vijos1901 学姐的钱包
  3. CVE-2014-0160 Heartbleed Vul Analysis && OpenSSL Cryptographic Software Library Bug
  4. iOS代码工具箱再续
  5. HD1847 Good Luck in CET-4 Everybody!(巴什博弈)
  6. 在visual studio2012中如何使用localDB具体讲解
  7. spring事务学习(转账案例)(一)
  8. Jquery 表单操作
  9. 用开源Look&Feel (Substance)写 漂亮的Swing应用程序
  10. oracle中Blob和Clob类型的区别