leetcode160
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
{
if (headA == null || headB == null)
{
return null;
}
else
{
var tempA = headA;
var tempB = headB; var listA = new List<ListNode>();
var listB = new List<ListNode>();
while (headA != null)
{
listA.Add(headA);
headA = headA.next;
}
while (headB != null)
{
listB.Add(headB);
headB = headB.next;
} listA.Reverse();
listB.Reverse(); if (listA.Count > listB.Count)
{
for (int i = ; i < listB.Count; i++)
{
if (listA[i].val != listB[i].val)
{
if (i > )
{
tempB = listB[i - ];
}
else
{
tempB = null;
}
break;
}
else
{
if (i > )
{
tempB = listB[i];
}
}
}
return tempB;
}
else
{
for (int i = ; i < listA.Count; i++)
{
if (listA[i].val != listB[i].val)
{
if (i > )
{
tempA = listA[i - ];
}
else
{
tempA = null;
}
break;
}
else
{
if (i > )
{
tempA = listA[i];
}
}
}
return tempA;
}
}
}
}
https://leetcode.com/problems/intersection-of-two-linked-lists/#/description
补充一个python的实现:
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
tempA = headA
tempB = headB
while tempA != tempB:
if tempA == None:
tempA = headB
else:
tempA = tempA.next
if tempB == None:
tempB = headA
else:
tempB = tempB.next
return tempA
leetcode160的更多相关文章
- [LeetCode160]Intersection of Two Linked Lists
题目: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- [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.相交链表
编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...
- leetcode-160周赛-5241-铺瓷砖
题目描述: 方法一:动态规划 class Solution: def f(self, n, m): if n < m: n, m = m, n if (n, m) in self.mem: re ...
- leetcode-160周赛-5240-串联字符串的最大长度
题目描述: 自己的提交:O(2**n∗n∗m),m 为字符串长度 class Solution: def maxLength(self, arr: List[str]) -> int: from ...
- leetcode-160周赛-5239-循环码排列
题目描述: 参考格雷编码: class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: res ...
- leetcode-160场周赛-5238-找出给定方程的正整数解
题目描述: class Solution: def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[Li ...
- LeetCode160 相交链表(双指针)
题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...
- 朋友遇到过的t厂面试题
朋友遇到过的t面试题 leetcode160 找链表交点 leetcode206 反转链表
随机推荐
- # 学号 20175223 《Java程序设计》第3周学习总结
学号 20175223 <Java程序设计>第3周学习总结 教材学习内容总结 第四章要点: 要点1:面向对象三个性质:封装性.继承.多态: 要点2:类:类声明.类体.成员变量.方法.类的U ...
- red hat防火墙的开启与关闭及状态查看方法
Redhat使用了SELinux来增强安全, 首先怎么查看防火墙的状态呢? a.可以通过如下命令查看iptables防火墙状态: chkconfig --list iptables b. selinu ...
- DisplayLink 安装错误
根据 在官网论坛上的反馈结果,程序自己有验证数字签名,数字签名验证不通过,即如上图所示.可能的原因:病毒:下载不完全:证书链出问题
- keil安装
安装准备: 一个keil安装程序,一个注册机 安装步骤.... 软件安装: 打开C51V901.EXE 安装程序 点击Next, 同意上述证书协议 重复点击Next,知道出现finish停下. 3个多 ...
- CentOS上升级gcc编译器使支持C++11
首先向博主致敬,好的东西拿来共享了,用一下不错. https://blog.csdn.net/clirus/article/details/62424517 0. 目标 最近在学习c++11,我本机 ...
- latex之插入向量、图片、编号
1.向量 $\vec a$\qquad $\overleftarrow{AB}$\qquad $\overleftrightarrow{AB}$\qquad $\overrightarrow{AB}$ ...
- Python全栈之路----函数----局部变量
全局变量是定义在函数外部以及代码的变量,全局能用. 局部变量就是定义在函数里的变量,只能在局部生效. 在函数内部,可以引用全局变量. 如果全局和局部都有一个名字相同的变量,在函数内会优先调用函数内的局 ...
- 巡风安装配置 -windows
巡风是一款适用于企业内网的漏洞快速应急.巡航扫描系统,通过搜索功能可清晰的了解内部网络资产分布情况,并且可指定漏洞插件对搜索结果进行快速漏洞检测并输出结果报表. 其主体分为两部分:网络资产识别引擎,漏 ...
- 如何避免提交页面,信息未填写完善 就出现注册成功提示 基于js
基于bootstrip做好一个页面后,出现如下效果图 这个页面是未经过任何后端处理的页面,如果直接填写一个用户名 或者不填写任何东西都可以注册成功的,先来演示只输入一个用户名 就要可以注册成功的. 点 ...
- [c#]_ELVE_Message多功能用法
1. 当要显示如图3个按钮时,并要获得单击不同按钮的进行不同的相应时,可以在MessageBoxButtons后面添加一个.(应该英文的点,此处为了醒目,用中文代替)可以看到提示框下方需要几个按 ...