160. Intersection of Two Linked Lists

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.

找到两个单链表相交的点。此处假设单链表不存在环。

思路:首先获得两个单链表的长度,得到长度的差值n,然后将指向长链表的指针A先向后移动n位,之后指针A同指向短链表的指针B同时每次移动一位,当A与B指向同一节点时,该节点就是相交的结点。

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == NULL || headB == NULL)
{
return NULL;
}
ListNode * a = headA;
ListNode * b = headB;
int n = ;
int m = ;
while(headA->next != NULL)
{
headA = headA->next;
n++;
}
while(headB->next != NULL)
{
headB = headB->next;
m++;
}
if(headA != headB)
{
return NULL;
}
if(n > m)
{
for(int i = ; i < n-m; i++)
{
a = a->next;
}
while(a)
{
if(a == b)
{
return a;
}
else
{
a = a->next;
b = b->next;
}
}
}
else
{
for(int i = ; i < m-n; i++)
{
b = b->next;
}
while(b)
{
if(a == b)
{
return a;
}
else
{
a = a->next;
b = b->next;
}
}
}
return NULL;
}
};

拓展:

http://www.cppblog.com/humanchao/archive/2015/03/22/47357.html

leetcode 160的更多相关文章

  1. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

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

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

  5. Java实现 LeetCode 160 相交链表

    160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...

  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 单向链表

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

  8. Java for 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 求两个链表的起始重复位置 --------- java

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

随机推荐

  1. Unity3D和OGRE资源管理机制

    转自:http://www.tuicool.com/articles/QbMjUn 游戏中通常有大量资源,如网格.材质.纹理.动画.着色器程序和音乐等,游戏引擎作为做游戏的工具,自然要提供良好的资源管 ...

  2. STL 库中的陷阱----一个难以察觉的 bug

    请找出下面程序的 bug? int maxProfit2(vector<int> &prices) { int local[3] = {0}; int global[3] = {0 ...

  3. TP验证

  4. SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件

    SQL SERVER 2008配置Database Mail –用SQL  数据库发邮件 https://blogs.msdn.microsoft.com/apgcdsd/2011/06/28/sql ...

  5. 查看oracle当前session

      怎样查看oracle当前的连接数呢?只需要用下面的SQL语句查询一下就可以了. #查看当前不为空的连接select * from v$session where username is not n ...

  6. 关于 Unix 用户权限及进程权限及 Saved set-user-id

    最近在看APUE,看到3.14节,fcntl的时候#include <fcntl.h>int fcntl(int fd, int cmd, .../* int arg */);出错返回-1 ...

  7. c数据结构栈的基本操作(字符逆序输出)

    线性栈 输入字符,再输出 #include "stdafx.h" #include<stdlib.h> #include<malloc.h> #define ...

  8. Morphia 学习一 注解

    http://blog.csdn.net/liumm0000/article/details/7535858 生命周期方法注解(delete没有生命周期事件)@PrePersist save之前被调用 ...

  9. sqlserver2008 ,只能选C盘目录,不能选其它盘目录

    数据库sql2008安装后,无论备份或还原,只能看到C盘,手工输入路径,错误提示如下:尝试打开或创建物理文件 'D:\数据库\db.mdf' 时,CREATE FILE 遇到操作系统错误 5(拒绝访问 ...

  10. C# 操作Word知识汇总

    转自:http://blog.csdn.net/jiutao_tang/article/details/6574740 1. 微软官方实例: 段落.表格.图表 HOW TO:利用 Visual C# ...