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;
}

Java for LeetCode 160 Intersection of Two Linked Lists的更多相关文章

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

  2. [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)

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

  3. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

  4. Java [Leetcode 160]Intersection of Two Linked Lists

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

  5. 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 ...

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

  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. (链表 双指针) 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. Leetcode 160 Intersection of Two Linked Lists 单向链表

    找出链表的交点, 如图所示的c1, 如果没有相交返回null. A:             a1 → a2                               ↘               ...

随机推荐

  1. ORACLE创建表空间、创建用户、更改用户默认表空间以及授权、查看权限

    Oracle创建用户.表空间.导入导出....命令 //创建临时表空间 create temporary tablespace ext_temptempfile 'D:\oracle\product\ ...

  2. BZOJ1045 [HAOI2008] 糖果传递

    Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n<=987654321,表示小朋友的个数 ...

  3. HDU 2242 考研路茫茫----空调教室

    传送门 考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. Nutch2.x 演示抓取第一个网站

    http://www.micmiu.com/opensource/nutch/nutch2x-crawl-first-website/?utm_source=tuicool&utm_mediu ...

  5. 初学Struts2-自定义拦截器及其配置

    自定义拦截器,首先新建一个继承自AbstractInterceptor类的类,然后重写intercept方法,代码如下 public class HelloInterceptor extends Ab ...

  6. DataTable列上多值运算

    1.从网上找了个中缀算法(也不知道什么前缀后缀,抱歉),可以对字符串表达式进行运算 2.有些时候还是会用到ASCII码表的 char c = expression[k];//expression为一字 ...

  7. ajax 如何做到 SEO 友好

    我猜你是在网络上搜索“ajax如何被搜索引擎收录”.“ajax SEO”.“ajax SEO友好”等关键词来到这里的.你可能已经很疲惫了,因为前段时间我也这样搜索,但是我发现搜索到的内容质量不高,有的 ...

  8. HDOJ 1907 John

    对于任意一个 Anti-SG 游戏,如果我们规定当局面中所有的单一游戏的 SG 值为 0 时,游戏结束,则先手必胜当且仅当:  (1)游戏的 SG 函数不为 0 且游戏中某个单一游戏的 SG 函数大于 ...

  9. 自动获取wordpress日志中的第一张图片作为缩略图

    图片在博客中算是吸引访客阅读欲望的一种方法,在日志列表如果有一张吸引力十足的图片作为缩略图,70%的游客会点击浏览具体的文章.既然那样,赶紧去加缩略图吧. 我们知道 WordPress 有个日志缩略图 ...

  10. cocos进阶教程(1)Lua调用自定义C++类和函数的最佳实践

    第一层:纯C环境下,把C函数注册进Lua环境 a.lua 文件 )) a.c 文件 #include <lua.h> #include <lualib.h> #include ...