leetcode 160

时间:2021-07-31 05:51:52

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&plus;&plus;版 - 剑指offer之面试题37:两个链表的第一个公共结点&lbrack;LeetCode 160&rsqb; 解题报告

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

  2. &lbrack;LeetCode&rsqb; 160&period; 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&period; 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. &lbrack;LeetCode&rsqb; 160&period; 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&period; 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&period; 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. mysql操作入门基础之对数据库和表的增删改查

    一.数据库管理-- 1.登陆数据库 mysql -u root -p; -- 2.查看数据库服务器所有数据库 SHOW DATABASES; -- 3.创建数据库 CREATE DATABASE My ...

  2. iOS之搜索框UISearchController的使用&lpar;iOS8&period;0以后替代UISearchBar&plus;display&rpar;

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  3. ArrayList&comma;Vector&comma;HashMap&comma;HashSet&comma;HashTable之间的区别与联系

    在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...

  4. js 创建List&lt&semi;Map&gt&semi; 这种格式的集合

    //赋值 var list_map = new Array(); for ( var i = 0; i < 10; i++) { list_map.push({baidux:'baidux'+i ...

  5. 邮件工具类--EmailUtil

    /* Copyright Notice ===================================================* * This file contains propri ...

  6. 转载linux c语言程序的Makefile编写

    对于程序设计员来说,makefile是我们绕不过去的一个坎.可能对于习惯Visual C++的用户来说,是否会编写makefile无所谓.毕竟工具本身已经帮我们做好了全部的编译流程.但是在Linux上 ...

  7. 使用openXML 不用插件导出excel

    注释很详细,不做解释了,有疑问可以提问 using System.IO; using System.Text; namespace iLIS.Common { /// <summary> ...

  8. Spring Security 登录校验 源码解析

    传统情况下,在过滤器中做权限验证,Spring Secuirty也是在Filter中进行权限验证. 创建并注册过滤器 package com.awizdata.edubank.config; impo ...

  9. Nginx&period;conf配置文件参数说明与优化

    参考连接:nginx 核心配置优化详解 先说下优化 1.nginx运行工作进程个数 worker_processes  1; Nginx进程,一般设置为和cpu核数一样(nginx启动后有多少个wor ...

  10. 九度OJ1122题-吃巧克力

    题目1122:吃糖果 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2453 解决:1957 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力 ...