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 反转链表
随机推荐
- 第二周例行报告psp
此作业要求详见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2127 (1)psp表 本周进度条 累计进度图 本周PSP饼状图
- Centos上SSH连接过慢原因
最近发现机房里有些centos机器进行ssh登陆非常慢且会超时,经过查看发现时GSPI认证过慢问题造成: 使用 ssh -v 发现 debug1: SSH2_MSG_SERVICE_ACCEPT re ...
- CSS权值
选择器权值 标签选择器:权值为1 类选择器和伪类:权值为10 ID选择器:权值为100 通配符选择器:权值为0 行内样式:权值为1000 !important规则: 可调整样式规则的优先级 添加在样式 ...
- SQLI DUMB SERIES-11
(1)检测构造方式 由此看出输入的用户名以及密码都被一对单引号所包含. 方法一: (2) 模拟真实环境,以用户的身份登录. (3)用burp抓包.开启抓包,输入用户名和密码,会自动跳到这个页面,右键, ...
- JVM垃圾回收算法解析
JVM垃圾回收算法解析 标记-清除算法 该算法为最基础的算法.它分为标记和清除两个阶段,首先标记出需要回收的对象,在标记结束后,统一回收.该算法存在两个问题:一是效率问题,标记和清除过程效率都不太高, ...
- (28)django的中间件(自定义中间件和防范跨站请求伪造攻击)-重要的概念
Django中间件和中间件不是同一种东西 什么是中间件:中间件是一个很大的概念,只要程序和程序之间还有一层程序,用来处理两个程序的整个交互过程的请求.数据等等就叫中间件 Django中间件:是介于re ...
- myelipse与maven结合配置
添加插件 指定maven中jar包默认仓库地址 maven中debug模式中断点关联不到项目代码的问题 解决方法参考文章:https://blog.csdn.net/urnot/article/det ...
- BEAM188和LINK180简单实例
简介 一开始想做一个绳索单元悬挂重物的仿真,其实想法很简单,但是在实现过程中却出现了很大的问题,纠结了很久,初步归结为:绳索单元在垂直其单元方向上受力,会导致其产生很大的变形,从而导致其不收敛.因此专 ...
- Javascript 香蕉分段吃(数组分隔)
Javascript 香蕉分段吃(数组分隔) function chunk(arr, size) { var newArr =[]; for(var i = 0; i < arr.length; ...
- Nginx做web服务器反向代理
实验目的 通过nginx实现反向代理的功能,类似apache反向代理和haproxy反向代理 工作中用nginx做反向代理和负载均衡的也越来越多了 有些公司从web服务器到反向代理,都使用nginx. ...