Intersection of Two Linked Lists(java)
2024-08-25 18:01:55
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;
}
}
最新文章
- css文件 引用后不起作用
- 【JavaScript】操作Canvas画图
- Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)
- 《On Lisp》第四章第三节图4.6中的rmapcar函数中展现的apply陷阱
- 修改ubuntu DNS的步骤/wget url报错: unable to resolve host address的解决方法
- zabbix服务器监控suse系统教程
- 怎样开启SQL数据库服务
- 利用Multipeer Connectivity框架进行WiFi传输-b
- mysql时间int日期转换
- 后台返回data直接在页面转换
- 【思路解析】discuz 帖子设置封面 setthreadcover 表pre_forum_threadimage
- CentOS安装常用软件
- SERVLET API 中 forward() 与 redirect()的区别?
- 一个开源Delphi分类组件推荐网页
- TCP closing a connection
- Ubuntu16.04部署python2和python3共存的Jupyter Notebook
- 利用 Blob 处理 node 层返回的二进制文件流字符串并下载文件
- Oracle数据库启动出现ORA-27101错误之ORA-19815处理方式及数据备份
- I/O模型系列之一:Linux I/O模型基本概念
- WINFROM窗体实现圆角
热门文章
- HDU 1084 - ACM
- C# 对类中的保护成员进行写操作(邀请大家拍砖)
- Retrieving the COM class factory for component with CLSID XX failed due to the following error: 80070005 拒绝访问。
- Proud Merchants(POJ 3466 01背包+排序)
- Taurus.MVC
- usb mass storage之旅
- WinForm 鼠标进入移开窗体事件,因子控件导致的误触发
- C语言的本质(38)——makefile之变量
- 非常棒的Java REST服务器栈
- 携程SQL面试题忘大牛解答解决思路