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:

  1. A: a1 a2

  2. c1 c2 c3

  3. 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.
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. int getListLength(ListNode* head){
  12. int len = ;
  13. while(head){
  14. len++;
  15. head=head->next;
  16. }
  17. return len;
  18. }
  19. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  20. int lenA = getListLength(headA);
  21. int lenB = getListLength(headB);
  22. ListNode* common = NULL,*startA=headA,*startB=headB;
  23. if(lenA<lenB){
  24. int diff = lenB-lenA;
  25. while(diff--) startB=startB->next;
  26. }else{
  27. int diff = lenA-lenB;
  28. while(diff--) startA=startA->next;
  29. }
  30. while(startA){
  31. if(startA == startB){
  32. common = startA;
  33. break;
  34. }
  35. startA=startA->next;
  36. startB=startB->next;
  37. }
  38. return common;
  39. }
  40. };

[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. 【SQL】大杂烩

    --------------------------------- 索引 --------------------------------- 语法: CREATE [索引类型] INDEX 索引名称 ...

  2. UMl概述(转)

    1. UML的组成 UML由视图(View).图(Diagram).模型元素(Model Element)和通用机制(General Mechanism)等几个部分组成. a) 视图(View): 是 ...

  3. .Net类型与JSON的映射关系

    首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而.Net中的类型比较多.到底它们是如何映射的呢? 总体来讲,Json ...

  4. Spring-----7、bean实例的创建方式及依赖配置

    转载自:http://blog.csdn.net/hekewangzi/article/details/45648579

  5. CAA调试

    在需要调试的Module(*.m)上右键,选择属性,命令位置选择你的framework目录    路径选择对应工程目录下的\intel_a(或者Win64    --    64位机器) 然后就可以尽 ...

  6. Dijkstra算法(迪杰斯塔拉算法)

    算法描述: Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最 ...

  7. AngularJS 不得不了解的服务 $compile 用于动态显示html内容

    项目中一度纠结与AngularJS如何动态显示不同的html内容. 本来是希望直接使用下面的语句来实现: <div> </div> 但是很尴尬的是,这样不能识别出html标签, ...

  8. c#局域网聊天软件的实现

    本软件是基于大学寝室局域网聊天的思路.c#源代码如下: using System; using System.Drawing; using System.Collections; using Syst ...

  9. Thinking in Java——集合(Collection)

    一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...

  10. filestream streamreader

    filestream是一个读取文件的stream,其本身也是支持read和write的,负责的对文件的读与写,而streamreader则是建立在对流的基础上的读,同时还有streamwrite ht ...