lhl
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
  • python

  • leetcode

    • 数组

    • 位运算

    • 动态规划

    • 链表

      • 21. 合并两个有序链表
      • 141. 环形链表
      • 142. 环形链表 II
      • 160. 相交链表
      • 203. 移除链表元素
      • 206. 反转链表
      • 234.回文链表
    • 栈

    • 树

  • 软件测试

  • Git

  • linux

  • 产品

  • MySql

  • docker

  • leetcode
  • list
2023-04-29

相交链表

https://leetcode.cn/problems/intersection-of-two-linked-lists/ (opens new window) 需要注意的点 1.链表a和b组合起来,如果有交点,

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:

        if not headA or not headB:
            return None
        
        pa = headA
        pb = headB

        while pa != pb:

            if pa:
                pa = pa.next
            else:
                pa = headB

            if pb:
                pb = pb.next
            else:
                pb = headA
        
        return pa
142. 环形链表 II
203. 移除链表元素

← 142. 环形链表 II 203. 移除链表元素→

最近更新
01
lhl learn notes
02
filter
06-09
03
decorator
06-09
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式