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 反转链表
随机推荐
- Altera FPGA SoC搭建步骤
Altera SoC 官方搭建指南: https://rocketboards.org/foswiki/Documentation/EmbeddedLinuxBeginnerSGuide 官方文档中除 ...
- 剖析 GSM 加密机制以及位置更新的过程
你有没有想过打开手机时会发生什么?它是如何以安全的方式与网络进行通信?几乎所有人都知道TCP / IP,并且可能许多人还是专家,但是谈到电信方面,很少有人知道它的内部原理. gsm中的消息结构是什么? ...
- oracle密码过期的修改
ALTER USER 用户名 IDENTIFIED BY 密码 ;
- 数据持久化系列之Mysql
一.命令行操作 1.显示所有库: show databases; 2.要操作某个库,比如库名: db_book:use db_book; 3.查看表的基本结构,比如表名: t_book:desc t_ ...
- Linux下修改Jenkins默认端口
我是自动安装的Jenkins,默认目录为 jenkins安装目录:/var/lib/jenkins jenkins日志目录:/var/log/jenkins/jenkins.logjenkins默认配 ...
- java面试题01
一.JAVA基础 1.简述你所知道的JAVA修饰符及各自的使用机制?(public.abstract.final.synchronized.super…) 01.public:允许所有客户访问 02. ...
- Nmap使用指南
一.目标指定 1.CIDR标志位 192.168.1.0/24 2.指定范围 192.168.1.1-255 192.168.1-255.1(任意位置) 3.IPv6地址只能用规范的IPv6地址或主机 ...
- github与github网站push神器
GitBook.Editor(全英文,无汉化) 链接: http://pan.baidu.com/s/1slIZ5jJ 密码: q9mw source tree (汉化中文) 本地需要安装git客户端 ...
- PythonStudy——函数的分类 Classification of functions
# PEP8:python写代码的规范 def fn(n1, n2): """ 函数的文档注释 :param n1: 第一个数 :param n2: 第二个数 :retu ...
- R语言中的字符串处理函数
内容概览 尽管R是一门以数值向量和矩阵为核心的统计语言,但字符串有时候也会在数据分析中占到相当大的份量. R语言是一个擅长处理数据的语言,但是也不可避免的需要处理一些字符串(文本数据).如何高 ...