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 反转链表
随机推荐
- ubuntu 16.04 编译安装 trl8291cu系列 无线网卡驱动
1 先 下载git包 和相关编译工具 sudo apt-get update sudo apt-get install git linux-headers-generic build-essentia ...
- Problem A: 类的初体验
Description 定义一个类Data,只有一个double类型的属性和如下3个方法: 1. void init(double d);——初始化属性值. 2. double getVal ...
- 说说VBA中的面向对象
对象是 Visual Basic 的结构基础,在 Visual Basic 中进行的所有操作几乎都与修改对象有关.Microsoft Word 的任何元素,如文档.表格.段落.书签.域等,都可用 Vi ...
- 团队项目(MVP------新能源无线充电管理网站)(总结)
经过了几个月的学习时间与团队的磨合以及一系列的困难之后,我们mvp小组一起完成了这个项目,内心也是十分激动和有成就感的.其实一开始基础并不好,很多都不知道,但是通过在慕课网上的学习以及老师严厉地督促下 ...
- 外网访问SQLServer数据库holer实现
外网访问内网SQLServer数据库 内网主机上安装了SQLServer数据库,只能在局域网内访问,怎样从公网也能访问本地SQLServer数据库? 本文将介绍使用holer实现的具体步骤. 1. 准 ...
- matrix_chain_order
to calculate the min step of multiplicate some matixs package dynamic_programming; public class matr ...
- HTML和CSS标签常用命名规则
1.Images 存放一些网站常用的图片: 2.Css 存放一些CSS文件: 3.Flash 存放一些Flash文件: 4.PSD 存放一些PSD源文件: 5.Temp 存放所有临时图片和其它文件: ...
- PS 给照片换背景
1. 打开一张照片,导入证件照 2. 点击选择 => 选择并遮住 (快捷键 command + option + r) 3. 点击快速选择工具,将属性设置里面的视图模式选择为洋葱皮,鼠标点击需要 ...
- redis 的备份策略,最好使用:RDB-AOF 混合持久化
相关资料: Redis 4.0 新功能简介:RDB-AOF 混合持久化:http://blog.huangz.me/2017/redis-rdb-aof-mixed-persistence.html ...
- [转]Windows下使用VS2015编译openssl库
转自:http://blog.csdn.net/alger_magic/article/details/52584171 目标:编译vs环境下openssl库 工具: 1. 编译环境win10+vs2 ...