/**
* 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的更多相关文章

  1. [LeetCode160]Intersection of Two Linked Lists

    题目: Write a program to find the node at which the intersection of two singly linked lists begins.   ...

  2. [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 ...

  3. LeetCode160.相交链表

    编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...

  4. leetcode-160周赛-5241-铺瓷砖

    题目描述: 方法一:动态规划 class Solution: def f(self, n, m): if n < m: n, m = m, n if (n, m) in self.mem: re ...

  5. leetcode-160周赛-5240-串联字符串的最大长度

    题目描述: 自己的提交:O(2**n∗n∗m),m 为字符串长度 class Solution: def maxLength(self, arr: List[str]) -> int: from ...

  6. leetcode-160周赛-5239-循环码排列

    题目描述: 参考格雷编码: class Solution: def circularPermutation(self, n: int, start: int) -> List[int]: res ...

  7. leetcode-160场周赛-5238-找出给定方程的正整数解

    题目描述: class Solution: def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[Li ...

  8. LeetCode160 相交链表(双指针)

    题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...

  9. 朋友遇到过的t厂面试题

    朋友遇到过的t面试题 leetcode160 找链表交点 leetcode206 反转链表

随机推荐

  1. go web framework gin 启动流程分析

    最主要的package : gin 最主要的struct: Engine Engine 是整个framework的实例,它包含了muxer, middleware, configuration set ...

  2. iOS 弹幕制作

    离职的最后一天,在公司学习下弹幕的制作.基于OC. 主要思路: 1.首先建一个弹幕类BulletView,基于UIView,然后在该类上写个UIlabel,用于放置弹幕文字,然后前端放置一个UIIma ...

  3. highcharts 获取不到隐藏容器大小

    1.固定图表大小 2.图表容器div的resize(绑定一个始终显示的,可以影响所有图表的) 影响容器大小改变的: 窗口大小改变 侧边栏切换 滚动条切换

  4. C语言笔记变量与数据类型

    目录 1.转义字符 2.常量与变量 2.1 什么是常量和变量 2.2 内存 2.3 变量的内存机制 2.4 变量命名规则 2.5 变量的定义 2.6 常量的定义 2.7 计算机内存字节顺序 2.8 局 ...

  5. s21day12 python笔记

    s21day12 python笔记 一.函数中高级 1.1 函数可以做返回值 #示例: def func(): print(123) def bar(): return func v = bar() ...

  6. linux samba smb 在客户端无法连接使用

    netstat -nutlp查看是否启动进程 1.网络是否正常,是否可以ping通.2.Windows和Linux的防火墙全部关闭.3.samba的端口是否开启,tcp/139.tcp/445.udp ...

  7. 在Linux中执行.sh脚本,异常

    在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...

  8. 开发中经常遇到SVN清理失败的问题:

    开发中经常遇到SVN清理失败的问题: 解决方法: step1: 到 sqlite官网 https://www.sqlite.org/download.html 下载 sqlite3.exe       ...

  9. Python基础-socketserver

    ocketserver通讯模块实现并发操作,基于select.epoll.socket.多线程,实现的正真多线程多并发 socketserver通讯模块底层调用的socket模块,只是它作了处理基于l ...

  10. [笔记]_ELVE_正则表达式

    本文章转载至简书技匠,简书签约作者,技匠,以上内容欢迎大家分享到朋友圈/微博等. 正则表达式,一个十分古老而又强大的文本处理工具,仅仅用一段非常简短的表达式语句,便能够快速实现一个非常复杂的业务逻辑. ...