题目

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

For example, the following two linked lists:



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.

分析

给定两个链表,求它们的交叉节点。

要求,时间复杂度在O(n)内,空间复杂度为O(1)

两个链表的长度不定,但是交叉节点的后续节点全部相同,所以先求得每个链表的长度lenA和lenB,将较长的链表先移动|lenA−lenB|个位置,然后同时后移,遇到的第一个值相等的节点既是要求的交叉节点。

AC代码

/**
* 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 || !headB)
return NULL; ListNode *p = headA, *q = headB; //求出输入两个链表的长度
int lenA = 0, lenB = 0;
while (p)
{
++lenA;
p = p->next;
}//while while (q)
{
++lenB;
q = q->next;
}//while //让长的链表先移动多出的节点
p = headA;
q = headB;
if (lenA > lenB)
{
int i = 0;
while (p && i < lenA - lenB)
{
p = p->next;
++i;
}//while
}
else{
int j = 0;
while (q && j < lenB - lenA)
{
q = q->next;
++j;
}//while
} while (p && q && p->val != q->val)
{
p = p->next;
q = q->next;
}//while return p;
}
};

GitHub测试程序源码

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. (LinkedList)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之“链表”:Intersection of Two Linked Lists

    此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...

  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(2个链表的公共节点)

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

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

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

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. 朱晔的互联网架构实践心得S2E7:漫谈平台架构的工作(基础架构、基础服务、基础平台、基础中间件等等)

    前言 程序开发毕竟还不是搬砖这种无脑体力劳动,需要事先有标准,有架构,有设计,绝对不是新公司今天创立,明天就可以开始编码的.其实很多公司在起步的时候没有财力和资源建设独立的基础架构或平台架构部门,甚至 ...

  2. rabbitmq 不发送ack消息如何处理: RabbitMQ 消息确认以及消息消费方处理消息时候抛出了异常以

    本篇的代码使用的前面两篇文章<RabbitMQ与Spring整合之消息生产方>和<RabbitMQ与Spring整合之消息消费方>的代码,这两篇文件里配置文件的名称不正确,不可 ...

  3. 在前端解決顯示null值一例

    用.net core2.1 api返回的JSON里包含null值,前端直接顯示出來了,影響閱讀, 用以下語句將null轉換為空字符串: // Build html. html += "< ...

  4. sublime text 3 入门技巧与常见问题解决

    1. 常见问题 - 解决sublime 窗口栏(UNREGISTERED)(未购买)导致的经常性弹窗 解决方法: 点击Help -> About Sublime Text,查看sublimete ...

  5. Azure 门户使用概览

    Azure 门户是管理 Azure 云平台的核心工具,用户可以在其中预配和管理 Azure 资源.本教程将帮助你熟悉Azure管理门户,包括一些关键功能的介绍,并演示了如何通过 Azure 门户创建虚 ...

  6. SQL 事物回滚

    转自https://www.cnblogs.com/delphinet/archive/2010/08/17/1801424.html 第一种:   declare   @iErrorCount    ...

  7. jacvaSe-LinkedList

    package com.java.chap08.sec02; import java.util.LinkedList; public class TestLinkedList { private st ...

  8. 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...

  9. 如何使用Java代码给图片增加倒影效果

    效果 倒影率为90%时的效果: 倒影率10%时的效果: 实现原理 倒影率作为参数rate 传入Reflection button的事件处理函数: CreateImageWithReflection这个 ...

  10. Winform C# 编程 1

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL01Wmfc0B-QfB2pS1y13peTbq