zoukankan      html  css  js  c++  java
  • [LeetCode] Intersection of Two Linked Lists

     

    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:

    • If the two linked lists have no intersection at all, return null.
    • The linked lists must retain their original structure after the function returns.
    • You may assume there are no cycles anywhere in the entire linked structure.
    • Your code should preferably run in O(n) time and use only O(1) memory.

    找出两个链表的交点
    如果没有交点则返回nullptr
    方法一:原本想到是使用两个栈存储链表的数值,然后通过弹出元素进行比较。由于题目要求只能使用O(1)的space肯定不行。所以换了一个思路想到只要分别求出两个链表的长度然后比较最后相同的位数的链表元素即可。

    /**
     * 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) {
            if (headA == nullptr || headB == nullptr)
                return nullptr;
            int lenA = getListLength(headA), lenB = getListLength(headB);
            if (lenA > lenB) {
                for (int i = 0; i < lenA - lenB; i++)
                    headA = headA->next;
            }
            else {
                for (int i = 0; i < lenB - lenA; i++)
                    headB = headB->next;
            }
            while (headA != headB) {
                headA = headA->next;
                headB = headB->next;
            }
            return headA;
        }
        
        int getListLength(ListNode* head) {
            int len = 0;
            while (head) {
                len++;
                head = head->next;
            }
            return len;
        }
    };
    // 59 ms


    方法二:在discuss中看到有一个奇妙的解法。就是用环的思想来考虑这个问题,使用两个指针分别同步遍历两个链表,如果有链表遍历结束则开始转到另一个链表头部接着遍历,直到遍历到两个链表值相同,这个值就是两个链表的交点。

    /**
     * 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) {
            if (headA == nullptr && headB == nullptr)
                return nullptr;
            ListNode* nodeA = headA;
            ListNode* nodeB = headB;
            while (nodeA != nodeB) {
                nodeA = nodeA ? nodeA->next : headB;
                nodeB = nodeB ? nodeB->next : headA;
            }
            return nodeA;
        }
    };
    // 43 ms
  • 相关阅读:
    Oracle插入特殊字符问题
    Oracle数据库中IN参数个数超过1000的问题
    解决tomcat 内存溢出问题
    IDEA 最新版本 破解教程(windows,mac皆可用)(新版本已失效)
    javascript将字符串中的多个空格替换为一个空格的正则实例
    删除一个表中重复的数据
    mac/linux 解决启动命令行出现declare问题
    iftop 安装以及相关参数及说明(转载自csdn)
    centos安装与卸载postgresql
    jquery实现自动补全邮箱地址
  • 原文地址:https://www.cnblogs.com/immjc/p/7872120.html
Copyright © 2011-2022 走看看