leetcode刷题系列----模式2(Datastructure 链表)- 160:Intersection of Two Linked List 相交链表

本文介绍了一种利用双指针解决相交链表问题的方法,并提供了Python、C++、C#及Java四种语言的实现代码。此算法思想可应用于多种链表问题,时间复杂度为O(m+n)。

leetcode刷题系列----模式2(Datastructure 链表)- 160:Intersection of Two Linked List 相交链表

Tips

  • 更多题解请见本系列目录
  • 此链表问题可以使用双指针解决,注意算法的复杂度为O(m+n),意味着2*(m+n)次遍历也属于该时间复杂度,不可狭隘的认为仅仅遍历m+n次链表节点。
  • 双指针确实是一个很大的算法思想类。

Python

# 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:
        pointA = headA
        pointB = headB
        if not pointA or not pointB: return None
        while pointA != pointB:
            if pointA: pointA = pointA.next
            else: pointA = headB
            if pointB: pointB = pointB.next
            else: pointB = headA
        return pointA

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* pointA = headA;
        ListNode* pointB = headB;
        if (pointA==nullptr || pointB==nullptr) return nullptr;
        while (pointA!=pointB)
        {
            if(pointA!=nullptr) pointA=pointA->next;
            else pointA=headB;
            if(pointB!=nullptr) pointB=pointB->next;
            else pointB=headA;
        }
        return pointA;
        
    }
};

C#

/**
 * 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) {
        ListNode pointA = headA;
        ListNode pointB = headB;
        if (pointA==null || pointB==null) return null;
        while (pointA!=pointB)
        {
            if(pointA!=null) pointA=pointA.next;
            else pointA=headB;
            if(pointB!=null) pointB=pointB.next;
            else pointB=headA;
        }
        return pointA;
    }
}

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pointA = headA;
        ListNode pointB = headB;
        if (pointA==null || pointB==null) return null;
        while (pointA!=pointB)
        {
            if(pointA!=null) pointA=pointA.next;
            else pointA=headB;
            if(pointB!=null) pointB=pointB.next;
            else pointB=headA;
        }
        return pointA;        
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值