LeetCode上面的题目例如以下:

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.

 * public class ListNode {

 *     int val;

 *     ListNode next;

 *     ListNode(int x) {

 *         val = x;

 *         next = null;

 *     }

 * }

 */

public class Solution {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        

int lengthA = 0,lengthB = 0;

for(ListNode head= headA;head!=null;head = head.next)

{

lengthA++;

}

for(ListNode head = headB;head!=null;head = head.next)

{

lengthB++;

}

if(lengthA>=lengthB)

{

for(int i=0;i<lengthA-lengthB;i++)

{

headA = headA.next;

}

}

else

{

for(int i=0;i<lengthB-lengthA;i++)

{

headB = headB.next;

}

}

for(ListNode newA =headA,newB = headB;newA!=null&&newB!=null;newA = newA.next,newB = newB.next)

{

if(newA==newB)

{

return newA;

}

}

return null;

    }

}

(LeetCode)两个链表的第一个公共节点的更多相关文章

  1. 剑指Offer面试题:31.两个链表的第一个公共节点

    一.题目:两个链表的第一个公共节点 题目:输入两个链表,找出它们的第一个公共结点. 链表结点定义如下,这里使用C#语言描述: public class Node { public int key; p ...

  2. 剑指offer-第五章优化时间和空间效率(两个链表的第一个公共节点)

    思路1:要求的是两个链表的第一个公共节点,首先想到的是用栈来存放两个链表,然后依次从栈中抛出,直到最后一个相同的节点为止.但是要用到两个栈,空间复杂度为O(n): 思路2:从头到尾分别遍历两个链表得到 ...

  3. 剑指 Offer 52. 两个链表的第一个公共节点 + 链表 + 第一个公共结点 + 双指针

    剑指 Offer 52. 两个链表的第一个公共节点 Offer_52 题目详情 题解分析 可以使用两个指针 node1,node2 分别指向两个链表 headA,headB 的头结点,然后同时分别逐结 ...

  4. 【剑指offer】52. 两个链表的第一个公共节点

    剑指 Offer 52. 两个链表的第一个公共节点 知识点:链表: 题目描述 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 示例 示例1: 输入:intersectVal = 8, l ...

  5. 力扣 - 剑指 Offer 52. 两个链表的第一个公共节点

    题目 剑指 Offer 52. 两个链表的第一个公共节点 思路1(栈) 若两个链表相遇,则从它开始相遇的地方到链表末尾应该都是相同的,那么我们可以将两个链表分别放入两个栈中,然后依次循环比较两个栈顶的 ...

  6. LeetCode 面试题52. 两个链表的第一个公共节点

    题目链接:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/ 输入两个链表 ...

  7. Intersection of Two Linked Lists(两个链表的第一个公共节点)

    来源:https://leetcode.com/problems/intersection-of-two-linked-lists Write a program to find the node a ...

  8. 剑指offer 面试题52. 两个链表的第一个公共节点

    这题之前leetcode做过,权当复习 首先这题没说是否一定有公共节点,如果代码可能因为这一点造成死循环的,需要提前验证所给两个链表是否有公共节点. 方法1:对于每一个list1的节点,遍历list2 ...

  9. 【剑指Offer】面试题52. 两个链表的第一个公共节点

    题目 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...

  10. 每日一题 - 剑指 Offer 52. 两个链表的第一个公共节点

    题目信息 时间: 2019-07-03 题目链接:Leetcode tag: 单链表 难易程度:简单 题目描述: 输入两个链表,找出它们的第一个公共节点. 示例: A: a1 -> a2 \ - ...

随机推荐

  1. iOS不用官方SDK实现微信和支付宝支付XHPayKit

    作者:朱晓辉Allen 链接:https://juejin.im/post/5a90dd3a6fb9a0634912b755 前言 前段时间由于项目需求,移除了项目中的微信支付SDK和支付宝支付SDK ...

  2. gradle ofbiz 16 开发环境搭建

    原 gradle ofbiz 16 开发环境搭建 2017年02月13日 10:59:19 阅读数:2702 1.安装jdk 2.配置jdk环境变量 3.eclipse 安装svn 插件 4.svn下 ...

  3. codeforces round375(div.2)题解

    首先吐槽一下这套题...为什么EF全是图论QAQ 过了ABCD四个题...F的并查集死磕了好久... 不过似乎rank还算乐观...(因为ABC都是一次过的QAQ) Problem A: 啥都不想说Q ...

  4. POJ3311 Hie with the Pie

    The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortu ...

  5. DataBinder.Eval值的判断

    原文发布时间为:2009-04-10 -- 来源于本人的百度文章 [由搬家工具导入] 问:如何对<%# DataBinder.Eval(Container.DataItem,"Ly_R ...

  6. MEF 注入[转载]

    领域服务的时候,用到MEF的注入有参构造函数的方法,your master was attracted,打算稍微深挖一下,这篇来对此知识点做个总结. 一.知识点回顾 MEF作为IOC的方式之一,它的主 ...

  7. [论文]Clustering-Based Ensembles as an Alternative to Stacking

    Clustering-Based Ensembles as an Alternative to Stacking 作者:Anna Jurek, Yaxin Bi, Shengli Wu, and Ch ...

  8. EOJ Monthly 2018.2

    A. 坑爹的售票机 题意 用\(1,5,10,25,50,100\)的纸币买\(n\)张单价为\(p\)的船票,且一次性最多买\(k\)张,求钱数恰好时最少需要多少张纸币. Hard: \(n,k,p ...

  9. 模块(python的标准库)

    在python中叫做模块,其他语言中叫做类库.python中的模块有三种:内置模块,第三方模块,自定义模块. 模块的使用: 先导入,import+模块名,再使用,模块名+函数名() .py文件与.py ...

  10. 设置jenkins的邮件通知功能

    1.进入系统配置页面配置邮件发送的SMTP 2. 进入项目配置页面,配置邮件通知:(每次不稳定构建时会邮件通知)