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

[Linked List]Intersection of Two Linked Lists的更多相关文章

  1. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

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

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

  3. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  4. LeetCode: Intersection of Two Linked Lists 解题报告

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

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

  6. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  7. LeetCode_160. Intersection of Two Linked Lists

    160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...

  8. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

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

随机推荐

  1. PHP学习笔记1.1——date()函数的多种用法,取出各种不同格式的时间,非常全面

    语法格式:date(string format.int timestamp); 参数一:format指定输出的日期和时间的格式,具体的参见下表; 参数二:timestamp是可选参数,是时间戳,如果不 ...

  2. a:hover span 隐藏/显示 问题

    :hover是我们在CSS设计中最常运用的伪类之一,许多绚丽效果的实现离不开伪类:hover,比如我们常见的纯CSS菜单.相册效果等等. 或许用了这么久的伪类:hover,还有部分朋友还不完全了解ho ...

  3. transform的2D部分,嗯…就这个标题了。

    上一次写了transition的内容,这次就写拼写很类似的另外一个属性transform好了……我英语差这件事就不要吐槽了,下面是正文,真的: transition是过渡,transform是变换. ...

  4. AjaxHelper的get和post请求的封装类

    最近在学习ajax的时候发现不断的调用get和post请求时,代码重复很多,有的公司会用自带的封装的方法,这个可以直接调用ajax的方法,但是在运用的时候我们也应该学习它是怎么封装的和一些原理性的东西 ...

  5. CMD下修改IP地址

    @echo off netsh interface ip set address name="本地连接" static 192.168.1.55 255.255.255.0 192 ...

  6. Ubuntu安装tftp服务器

    一.安装如下软件包: sudo apt-get install xinetd tftpd tftp 二.在/etc/xinetd.d/目录下创建tftp文件,并输入如下内容. 执行命令:sudo vi ...

  7. 龙芯3A上V8的编译与测试

    使用平台: loongson3a+debian6.0.3+linux2.6.36.3+gcc4.6.3 一: V8的下载 这里V8是从其官网上使用git下载的: (1)如果没有git和git-svn需 ...

  8. 正式学习React (七) react-router 源码分析

    学习react已经有10来天了,对于react redux react-redux 的使用流程和原理,也已经有一定的了解,在我上一篇的实战项目里,我用到了react-route,其实对它还只是 停留在 ...

  9. 【配置】如何配置Tp-link无线路由器作为无线交换机

    最近刚装了电信天翼宽带,谁知道现在只能装光猫.你说一个4M带宽有必要用光猫么?装好之后问题来了,这个光猫很奇葩,只有一个以太网接口,确有内置有路由功能,也就是只有一个出口的路由器.现在问题来了:我想接 ...

  10. perl 调用按钮输出到文本框

    sub push_b4 { #$txt -> insert('end'); #select $txt; system("expect c:\\\\expect.txt >expe ...