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 反转链表
随机推荐
- React项目新手指南
对于程序员而言:驼峰和下划线之间是一场宗派战争:大括号是否换行会成为一种党派:逗号写在行尾还是行首的人来自不同星球…… 然而,无规矩不成方圆,任何一个团队,要想有高质量的产出,第一步必须要对一些基本的 ...
- 马凯军201771010116《面向对象程序设计(java)》第四周学习总结
第一部分:理论知识学习部分 第四章 1.类与对象的基础概念. 对象:即数据,对象有三个特性:行为 .状态.标识. 类是对象,事物的描述和抽象,是具有相同属性和行为的对象集合.对象则是该类事物的实例. ...
- XCode9和10编译Cordova报错toLowerCase of undefined解决方案
XCode升级到9或10后,cordova build时报错:toLowerCase of undefined... 解决方案: 修改platforms/ios/cordova/lib/list-em ...
- day 08 字符编码和文件的读写操作
打开文件的语法 f=open("test.txt",encoding=:"utf-8") #给系统发送一个指令,让操作系统去打开文件 使用上面的方法打开文件的方 ...
- C#中字符串大小比较函数--CompareTo与Compare方法(需要完善补充)
字符串比较的原理是什么? 原理: 从两个字符串的第一个字符开始逐个进行比较(按字符的ASCII值进行大小比较),直到出现不同的字符或遇到‘\0’为止. 如果全部字符都相同,就认为两字符串相等,返回0: ...
- tomcat 启动脚本走过的坑
最近由于 程序写的问题 tomcat经常需要重启,所以就让我写给监控tomcat并启动的脚本 例: 看着一起正常 然后就写到 crontab 计划任务里了 后来发现不管用, 找了好多文档 就是找不到问 ...
- 测试那些事儿—selenium自动化实战之登录验证码处理
登陆时经常出现验证码自动化测试如何处理呢? 一般有如下几种处理思路: 1.通过接口请求,拿到对应验证码信息 2.让开发配合把验证码改成万能验证码 3.注入cookies 如何通过注入cookies的方 ...
- python学习之路05
控制流语句 博主认为所有的语言中,控制语句都是差不多的,无非就是循环,判断,if ,while,for.更重要的是,多加的练习,实战中发现自身问题,加深巩固 所以,下面会以实际的题目为主. 1.用户在 ...
- 12.python-metaclass元类
1.python中一切皆是对象,类本身也是一个对象,当使用关键字class的时候,python解释器在加载class的时候会创建一个对象(这里的对象指的是类而非类的实例) class Foo: #cl ...
- 全志A33 lichee 开发板 Linux中断编程原理说明
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 本节实验目标实现按键触发中断 ...