LeetCode160.相交链表
编写一个程序,找到两个单链表相交的起始节点。
例如,下面的两个链表:
- A: a1 → a2
- ↘
- c1 → c2 → c3
- ↗
- B: b1 → b2 → b3
在节点 c1 开始相交。
注意:
- 如果两个链表没有交点,返回
null
. - 在返回结果后,两个链表仍须保持原有的结构。
- 可假定整个链表结构中没有循环。
- 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
- /**
- * 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;
- }
- int lenA = len(headA);
- int lenB = len(headB);
- if (lenA>lenB) {
- while (lenA != lenB) {
- headA = headA.next;
- lenA--;
- }
- } else {
- while (lenA != lenB) {
- headB = headB.next;
- lenB--;
- }
- }
- while (headA != headB) {
- headA = headA.next;
- headB = headB.next;
- }
- return headA;
- }
- private int len(ListNode headA) {
- int len = 0;
- while (headA != null) {
- headA = headA.next;
- len++;
- }
- return len;
- }
- }
- /**
LeetCode160.相交链表的更多相关文章
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode160 相交链表(双指针)
题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...
- 【LeetCode题解】160_相交链表
目录 160_相交链表 描述 解法一:哈希表 思路 Java 实现 Python 实现 解法二:双指针(推荐) 思路 Java 实现 Python 实现 160_相交链表 描述 编写一个程序,找到两个 ...
- LeetCode 160: 相交链表 Intersection of Two Linked Lists
爱写Bug(ID:iCodeBugs) 编写一个程序,找到两个单链表相交的起始节点. Write a program to find the node at which the intersectio ...
- LeetCode 160——相交链表(JAVA)
编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- leetcode腾讯精选练习之相交链表(六)
相交链表 题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5 ...
- Java实现 LeetCode 160 相交链表
160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...
- 【原创】leetCodeOj --- Intersection of Two Linked Lists 解题报告(经典的相交链表找交点)
题目地址: https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 题目内容: Write a program to fi ...
- LeetCode 160 相交链表
题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], li ...
随机推荐
- 关于Java程序流程控制的整理(已完善)
- AudioUnit录音和播放同时进行的一些注意点
录音(播放)和暂停 -(void)start { self.soundTotalLength = 0.0f; if (!self.unitHaveStart) { NSError *error = n ...
- day12:装饰器的进阶
1,三元运算符:变量 = 条件返回True的结果 if 条件 else 条件返回false的结果:必须要有结果:必须要有if和else,只能是简单的情况. 2,传参包起来,星号打散 def outer ...
- PHP之null
null类型 特殊的null值表示一个变量没有值.null类型唯一可能的值是null. 在下列情况下一个变量被认为是null: ①.被赋值为null ②.尚未被赋值 ③被unset(). 语法 nul ...
- Web开发——JavaScript基础
参考学习: MDN JavaScript:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript ECMAScript 6入门(阮一峰):htt ...
- [daily][pcaman] pacman滚动升级跳过指定包
有时候有些包是坏的, 我们在Syu的时候,想把它跳过去. 可以在pacman.conf 中使用如下选项: IgnorePkg=linux https://wiki.archlinux.org/ind ...
- Flink - StreamJob
先看最简单的例子, final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironmen ...
- CloudStack+KVM环境搭建(步骤很详细,说明ClockStack是用来管理虚拟机的)
文章目录环境准备配置本地域名解析关闭selinux安装ntp服务安装管理端安装Mysql数据库安装服务端RPM:初始化CloudStack数据库:初始化cloudstack管理服务器安装系统虚拟机安装 ...
- python如何使用request爬取图片
下面是代码的简单实现,变量名和方法都是跑起来就行,没有整理,有需要的可以自己整理下: image2local: import requests import time from lxml import ...
- Redis入门到高可用(六)—— 字符串
一.结构和命令 1.字符串键值结构 key是字符串,value可以是字符串.数字.二进制.json等: redis的key和string类型value限制均为512MB. 2.使用场景 ♦️ 缓存 ♦ ...