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. shell将输入的参数逆序

    #! /usr/bin/ksh count=$#  //总共输入参数 cmd="echo" while [[ $count -gt 0 ]] do cmd="$cmd \ ...

  2. 高斯混合聚类及EM实现

    一.引言 我们谈到了用 k-means 进行聚类的方法,这次我们来说一下另一个很流行的算法:Gaussian Mixture Model (GMM).事实上,GMM 和 k-means 很像,不过 G ...

  3. 【bzoj1061】 Noi2008—志愿者招募

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 (题目链接) 题意 给定n天,第i天需要ai个志愿者,有m类志愿者,每类志愿者工作时间为[l, ...

  4. SQLServer复制表

    把a的表结构复制到a1表,1=2不复制数据,如果要复制数据,就不要whereselect * into a1 from a where 1=2注意:这种方式不能复制主键.索引等信息如果要全部复制,只能 ...

  5. PowerDesigner导出的sql中去掉双引号,主要用于Oracle

    如题,这些双引号对于Oracle建表实在是糟糕透了: 解决方法: (转载,作者的百度已经挂了,无法放上链接,自私一把,以前的Oracle项目不见了,无法展示) 1.去掉Oracle生成的SQL创建语句 ...

  6. HYSBZ - 2152 聪聪和可可

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  7. event driven的一些概念

    1. event :Something that happens during your application that requires a response. 2.event object:Th ...

  8. python scrapy 获取华为应用市场APP评论数据

    scrapy入门 四步: 1. 创建一个新的Scrapy Project 2. 定义你需要从网页中提取的元素Item 3. 实现一个Spider类,通过接口完成爬取URL和提取Item的功能 4. 实 ...

  9. spark中操作hdfs

    1 获取路径 val output = new Path("hdfs://master:9000/output/"); val hdfs = org.apache.hadoop.f ...

  10. MyEclipse代码提示快捷键和常用设置

    我使用的是MyEclipse 6.0版本,代码助手(content assist)的快捷键由 Alt + / 改成了 Ctrl + Space,恰好我的输入法快捷键也是 Ctrl + Space .造 ...