Description:

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.

求两个链表的相交的部分

/**
* 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) {
if(headA==null || headB==null)
return null;
ListNode aNode = headA;
ListNode bNode = headB;
int aLen = 0, bLen = 0;
while(aNode != null) {
aLen ++;
aNode = aNode.next;
}
while(bNode != null) {
bLen ++;
bNode = bNode.next;
}
if(aNode != bNode)
return null;
if(aLen > bLen) {
for(int i = 0; i < aLen-bLen; i++)
headA = headA.next;
}
else if(aLen < bLen) {
for(int i=0; i<bLen-aLen; i++)
headB = headB.next;
}
while(headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}
}

LeetCode——Intersection of Two Linked Lists的更多相关文章

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

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

  3. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

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

  5. LeetCode Intersection of Two Linked Lists (找交叉点)

    题意: 给两个链表,他们的后部分可能有公共点.请返回第一个公共节点的地址?若不重叠就返回null. 思路: 用时间O(n)和空间O(1)的做法.此题数据弱有些弱. 方法(1)假设两个链表A和B,用两个 ...

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

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

  8. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

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

  9. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

随机推荐

  1. Lua 中pairs与ipairs区别

    local tmp_tab = {}; tmp_tab[]="lua"; tmp_tab[]="hello" tmp_tab[]="aaa" ...

  2. Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values;

    项目部署,启动过程中有以下警告: [WARN]: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 41 ...

  3. ★ Maven的坑,tomcat插件6 不能与jdk8一起使用

    Maven 集成Tomcat7插件 maven WEB项目启动没问题访问页面就报错:org.apache.jasper.JasperException: Unable to compile class ...

  4. EMS快递单号生成算法

    <?php function emsnum($ems, $num) { $fri = substr($ems, 2, 8); $head = substr($ems, 0, 2); $tail ...

  5. [从jQuery看JavaScript]-注释(comments)

    jQuery片段: /*! * jQuery JavaScript Library v1.3.2 * http://jquery.com/ * * Copyright (c) 2009 John Re ...

  6. 关于Cocos2d-x中监听物体不超越边界的解决方案

    写一个监听器 touchlistener->onTouchMoved = [this](Touch* pTouch, Event*) { auto delta = pTouch->getD ...

  7. DirectoryEntry 账户启动与停用 以及创建账户等

    启动账户: DirectoryEntry usr = new DirectoryEntry("LDAP://CN=New User,CN=users,DC=fabrikam,DC=com&q ...

  8. android 创建 xml文件

    android创建xml文件的方法. 要操作android的外部存储,所以要在AndroidManifest.xml文件中添加权限. <uses-permission android:name= ...

  9. 第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表

    python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表 sadd(name,values)name对应的集合中添加元素 #!/usr/bin/env python # -*- ...

  10. C++ 程序可以定义为对象的集合

    C++ 基本语法C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互.现在让我们简要地看一下什么是类.对象,方法.即时变量. 对象 - 对象具有状态和行为.例如:一只狗的状态 - 颜色 ...