首页 > 其他 > 详细

???Intersection of Two Linked Lists

时间:2015-09-25 21:46:25      阅读:270      评论:0      收藏:0      [点我收藏+]

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.

 1、自己算法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    if (head == NULL)
        return NULL;
    struct ListNode *tmp1 = head->next;    //这里使用了head->next;所以head == NULL判断必须放上面
    struct ListNode *tmp2 = head;
    
    if(head->next == NULL)
        return head;
    head = head->next;
    tmp2->next = NULL;
    while(head->next != NULL)
    {
        head = head->next; 
        tmp1->next = tmp2;
        tmp2 = tmp1;
        tmp1 = head;
    }
    head->next = tmp2;
    return head;
}
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *tailA = reverseList(headA);
    struct ListNode *tailB = reverseList(headB);
    struct ListNode *tmpA = NULL;
    struct ListNode *tmpB = NULL;
    struct ListNode *tmpA2 = tailA;
    struct ListNode *tmpB2 = tailB;
    while(tailA && tailB)
    {
        if(tailA == tailB)
        {
            tmpA = tailA;
            tmpB = tailB;
            tailA = tailA->next;
            tailB = tailB->next;
        }
        else
        {
            reverseList(tmpA2);
            reverseList(tmpB2);
            return tmpA;
        }
    }
    return NULL;
}
  • 先把两个链表倒过来,然后从后向前比较,找到第一个不相同的点停止,把链表再倒回来,结果出错
  • 原来问题出在了这两个链表不是完全独立的链表,所以两个链表都倒转一遍的时候,就会出错,纸上画画就知道了

2、使用网上的方法

 

???Intersection of Two Linked Lists

原文:http://www.cnblogs.com/dylqt/p/4839520.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!