Intersection of Two Linked Lists(java)
eg: a1 → a2
↘
c1 → c2 → c3 或者直接a1 → b1
↗
b1 → b2 → b3
求公共链表c1. 常规的指针分裂法,复制法,偏移法。
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null) return null;
int lengthA=0;
int lengthB=0;
ListNode currentA=headA;
ListNode currentB=headB;
while(currentA.next!=null)
{
currentA=currentA.next;
lengthA++;
}
while(currentB.next!=null)
{
currentB=currentB.next;
lengthB++;
}
ListNode fast=lengthA>lengthB?headA:headB;
ListNode slow=lengthA>lengthB?headB:headA;
for(int i=0;i<Math.abs(lengthA-lengthB);i++)
{
fast=fast.next;
}
while(fast!=null)
{
if(fast==slow)
return fast;
else
{
fast=fast.next;
slow=slow.next;
}
}
return null;
}
}
Intersection of Two Linked Lists(java)的更多相关文章
- 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 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- [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 ...
- 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 ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- 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 ...
随机推荐
- java 去除重复项
import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Demo20 { public stati ...
- Web 1三级联动 下拉框 2添加修改删除 弹框
Web 三级联动 下拉框 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- css3实现手机qq空间菜单按钮
工作之余写的一个类似于QQzone的菜单效果 先上截图: 图一为点击按钮前界面: 图二为点击按钮后的界面 下面上代码: <!--css部分--> <style type=" ...
- C# 对类中的保护成员进行写操作(邀请大家拍砖)
假如我有一个类库 Lib,提供一个类 ClassA 对外服务,ClassA 中有若干只读属性 PropA, PropB 等等,外部调用者无法对 ClassA 中的 PropA 和 PropB 进行写操 ...
- PROCEDURE_监测系统_告警信息存储过程—产生告警信息插入告警表
create or replace procedure proc_alarmlog(in_id in number, --采集器编码 ...
- http://codepen.io/zhou-yg/pen/NqgPmg 在线编辑器
http://codepen.io/zhou-yg/pen/NqgPmg 在线编辑器
- Python修改文件名
Python批量修改文件名 # -*- coding: cp936 -*- import os from nt import chdir path="./files/" froms ...
- 1008 Gnome Tetravex
练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. #include <stdio.h> ][],note[],ans[]; void ini(){ int ...
- information_schema.triggers 学习
mysql实例中的每一个trigger 对应到information_schema.triggers 中有一行 1.information_schema.triggers 表的常用列: 1.trigg ...
- Android 中常用代码片段
一:AsyncTask 的使用 (1)activity_main.xml <TextView android:id="@+id/tvInfo" android:layout_ ...