leetcode 160. Intersection of Two Linked Lists 链表公共节点

来源:互联网 发布:带着淘宝去古代书包网 编辑:程序博客网 时间:2024/06/09 17:16

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.

这道题要求找到公共的结点的起点,可以将A,B两个链表看做两部分,交叉前与交叉后。交叉后的长度是一样的,因此交叉前的长度差即为总长度差。只要去除这些长度差,距离交叉点就等距了。为了节省计算,在计算链表长度的时候,顺便比较一下两个链表的尾节点是否一样,若不一样,则不可能相交,直接可以返回NULL。

我最初的想法是两两比较结点,但是复杂度太高。

代码如下:

/*class ListNode {      int val;      ListNode next;      ListNode(int x) { val = x; next = null; }}*//* * 可以将A,B两个链表看做两部分,交叉前与交叉后。 * 交叉后的长度是一样的,因此交叉前的长度差即为总长度差。 * 只要去除这些长度差,距离交叉点就等距了。 * 为了节省计算,在计算链表长度的时候,顺便比较一下两个链表的尾节点是否一样, * 若不一样,则不可能相交,直接可以返回NULL * */public class Solution{    public ListNode getIntersectionNode(ListNode headA, ListNode headB)     {        if(headA == null || headB == null)            return null;        ListNode iter1=headA;        int len1=1;        while(iter1.next!=null)        {            len1++;            iter1=iter1.next;        }        ListNode iter2=headB;        int len2=1;        while(iter2.next!=null)        {            len2++;            iter2=iter2.next;        }        if(iter1!=iter2)            return null;        else        {            iter1=headA;            iter2=headB;            if(len1>=len2)            {                for(int i=0;i<len1-len2;i++)                    iter1=iter1.next;            }else            {                for(int i=0;i<len2-len1;i++)                    iter2=iter2.next;            }            while(iter1!=iter2)            {                iter1=iter1.next;                iter2=iter2.next;            }            return iter1;        }    }}

下面是C++的做法,就是做一次遍历即可

代码如下:

#include <iostream>#include <algorithm>#include <vector>using namespace std;/*struct ListNode {     int val;     ListNode *next;     ListNode(int x) : val(x), next(NULL) {}};*/class Solution {public:    ListNode *getIntersectionNode(ListNode *h1, ListNode *h2)     {        int len1 = getLength(h1);        int len2 = getLength(h2);        ListNode* i = h1;        ListNode* j = h2;        if (len1 >= len2)        {            for (int k = 0; k < len1 - len2; k++)                i = i->next;        }        else        {            for (int k = 0; k < len2 - len1; k++)                j = j->next;        }        while (i!=NULL && j!=NULL && i != j)        {            i = i->next;            j = j->next;        }        if (i == NULL || j == NULL)            return NULL;        else            return i;    }    int getLength(ListNode *h)    {        int len = 0;        while (h != NULL)        {            len++;            h = h->next;        }        return len;    }};
阅读全文
0 0
原创粉丝点击