码迷,mamicode.com
首页 > 其他好文 > 详细

160. 相交链表

时间:2020-03-02 20:41:45      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:节点   alt   ble   stat   toc   编写   info   技术   tin   

160. 相交链表

1、试题介绍

编写一个程序,找到两个单链表相交的起始节点。

技术图片

试题链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/

2、java做法

2.1、双重循环

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) return null;
        while (headA != null) {
            ListNode p = headB;
            while (p != null) {
                if(headA == p) {
                    return headA;
                }
                p = p.next;
            }
            headA = headA.next;
        }
        return null;
    }

测试结果:

技术图片

2.2、双指针做法

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) return null;

        ListNode pA = headA;
        ListNode pB = headB;

        int sizeA = 0;
        int sizeB = 0;
        while (pA != null || pB != null) {
            if(pA != null) {
                sizeA++;
                pA = pA.next;
            }
            if(pB != null) {
                sizeB++;
                pB = pB.next;
            }
        }

        pA = headA;
        pB = headB;
        if(sizeA > sizeB) {
            for(int i = 0;i < sizeA - sizeB;i++) {
                pA = pA.next;
            }
        }else {
            for(int i = 0;i < sizeB - sizeA;i++) {
                pB = pB.next;
            }
        }

        while (pA != pB ) {
            if(pA == null || pB == null) return null;

            pA = pA.next;
            pB = pB.next;
        }

        return pA;
    }

测试结果:

技术图片

3、C语言做法

3.1、双重循环

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
        if(headA == NULL || headB == NULL) return NULL;
        while (headA != NULL) {
            struct ListNode* p = headB;
            while (p != NULL) {
                if(headA == p) {
                    return headA;
                }
                p = p->next;
            }
            headA = headA->next;
        }
        return NULL;
}

测试结果:

技术图片

3.2、双指针做法

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(headA == NULL || headB == NULL) return NULL;

    struct ListNode* pA = headA;
    struct ListNode* pB = headB;

    int sizeA = 0;
    int sizeB = 0;
    while (pA != NULL || pB != NULL) {
        if(pA != NULL) {
            sizeA++;
            pA = pA->next;
        }
        if(pB != NULL) {
            sizeB++;
            pB = pB->next;
        }
    }

    pA = headA;
    pB = headB;
    if(sizeA > sizeB) {
        for(int i = 0;i < sizeA - sizeB;i++) {
            pA = pA->next;
        }
    }else {
        for(int i = 0;i < sizeB - sizeA;i++) {
            pB = pB->next;
        }
    }

    while (pA != pB ) {
        if(pA == NULL || pB == NULL) return NULL;

        pA = pA->next;
        pB = pB->next;
    }

    return pA;
}

测试结果:

技术图片

160. 相交链表

标签:节点   alt   ble   stat   toc   编写   info   技术   tin   

原文地址:https://www.cnblogs.com/xgp123/p/12397561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!