作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/intersection-of-two-linked-lists/description/

题目描述

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

For example, the following two linked lists:

A:          a1 → a2

                     c1 → c2 → c3

B:     b1 → b2 → b3
begin to intersect at node c1.

Notes:

  1. If the two linked lists have no intersection at all, return null.
  2. The linked lists must retain their original structure after the function returns.
  3. You may assume there are no cycles anywhere in the entire linked structure.
  4. Your code should preferably run in O(n) time and use only O(1) memory.
    Credits:
  5. Special thanks to @stellari for adding this problem and creating all test cases.

题目大意

找出两个链表的最早公共元素。

解题方法

双指针

第一次遍历时,如果两者的非公共元素的个数正好相等,那么一定能找到相同元素;如果非公共元素个数不等,那么在一次遍历之后,两者的指针的差距就是非公共元素的个数差。这样翻转之后,指针的差距正好弥补了非公共元素的差,这样,第二次遍历要么一定相遇,要么两者没有公共元素,返回None。

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if headA is None or headB is None:
return None
pA = headA
pB = headB
while pA is not pB:
pA = headB if pA is None else pA.next
pB = headA if pB is None else pB.next
return pA

二刷的时候,感觉写的解法更为容易理解。

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
len1, len2 = 0, 0
moveA, moveB = headA, headB
while moveA:
len1 += 1
moveA = moveA.next
while moveB:
len2 += 1
moveB = moveB.next
if len1 < len2:
for _ in range(len2 - len1):
headB = headB.next
else:
for _ in range(len1 - len2):
headA = headA.next
while headA and headB and headA != headB:
headA = headA.next
headB = headB.next
return headA

因为后面的元素是相等的,所以使用栈把相等元素都弹出来,那么不等元素就是所求。

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
stack1, stack2 = [], []
while headA:
stack1.append(headA)
headA = headA.next
while headB:
stack2.append(headB)
headB = headB.next
pre = None
while stack1 and stack2:
s1 = stack1.pop()
s2 = stack2.pop()
if s1 != s2:
return pre
else:
pre = s1
return pre

日期

2017 年 8 月 27 日
2018 年 11 月 26 日 —— 11月最后一周!

【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)的更多相关文章

  1. [LeetCode] 160. Intersection of Two Linked Lists 解题思路

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. LeetCode: Intersection of Two Linked Lists 解题报告

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  3. [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  4. Java for LeetCode 160 Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. Java [Leetcode 160]Intersection of Two Linked Lists

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

  6. LeetCode 160. Intersection of Two Linked Lists (两个链表的交点)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. Leetcode 160. Intersection of two linked lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

随机推荐

  1. DNS域名解析全过程

    一张图看懂DNS域名解析全过程   DNS域名解析是互联网上非常重要的一项服务,上网冲浪(还有人在用这个词吗?)伴随着大量DNS服务来支撑,而对于网站运营来说,DNS域名解析的稳定可靠,意味着更多用户 ...

  2. PPT——一个有情怀的免费PPT模板下载网站!“优品PPT”

    http://www.ypppt.com/ PS:再推荐一款免费PPT下载网站 https://www.v5ppt.com/ppt-5-42-1.html

  3. Excel—分组然后取每组中对应时间列值最大的或者最小的

    1.MAX(IF(A:A=D2,B:B)) 输入函数公式后,按Ctrl+Shift+Enter键使函数公式成为数组函数公式. Ctrl+Shift+Enter: 按住Ctrl键不放,继续按Shift键 ...

  4. 10.Power of Two-Leetcode

    Given an integer, write a function to determine if it is a power of two. class Solution { public: bo ...

  5. 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍

    这次我尝试写一个原创的项目 the_game 框架选择: SpringBoot+Mybatisplus+Shiro 首先是简单的介绍(素材灵感来自英雄联盟) 5个关键的表: admin(管理员): l ...

  6. 工作学习2-gcc升级引发的崩溃

    分享一下调查gcc 8.0下,函数漏写返回值崩溃问题,调查记录. 现在新的硬件,基本操作系统都是redhat 8.0,升级后测试时,发现了一个崩溃问题,记录一下. ================== ...

  7. 100个Shell脚本——【脚本7】批量建立用户

    [脚本7]批量建立用户 编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组. 一.脚本 #!/bin/bash group=`c ...

  8. Output of C++ Program | Set 12

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  9. When does compiler create default and copy constructors in C++?

    In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...

  10. JS21. 使用原生JS封装一个公共的Alert插件(HTML5: Shadow Dom)

    效果预览 Shadow DOM Web components  的一个重要属性是封装--可以将标记结构.样式和行为隐藏起来,并与页面上的其他代码相隔离,保证不同的部分不会混在一起,可使代码更加干净.整 ...