Intersection of Two Linked Lists

原创 2016年05月31日 11:42:04

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:

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

本人的思路:自以为机智地将两个链表反转,然后从链表的最末尾开始比较,需找到第一个不一样的节点,其上一个节点即使两个链表的交叉节点:
JAVA代码:

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

       // 解题思路:首先,将两个数组反转,然后,依次比较,找到第一个不一样的链表节点,输出第一个不一样的节点的上一节点即可。
         ListNode head1=headA;   //保存原链表的头节点
         ListNode head2=headB;   //保存原链表的头节点
         ListNode interNode;
         int num=0;             //用于计算非交叉的节点数
         if(head1==null||head2==null)//其中一个链表为空都返回空
         {
             return null;
         }
         ListNode headAReverse=reverseList( head1);  //将链表A反转
         ListNode headBReverse=reverseList(head2);  //将链表B反转
        if(headAReverse!=headBReverse)   //从原链表尾部开始比较
        {
            return null;
        }else{

            while((headAReverse.next==headBReverse.next)&&(headAReverse.next!=null)&&(headBReverse.next!=null))   //若两链表间的节点相同则继续比较
            {
               headAReverse=headAReverse.next;
               headBReverse=headBReverse.next;
            }
             interNode = headAReverse;//保存链表的交叉节点
             while(headAReverse!=null)//计算非交叉节点数目
             {
                 headAReverse=headAReverse.next;
                  num++;
             }
             for(int i=0;i<num;i++)
             {
                 head1=head1.next;  //依次寻找交叉节点的位置
             }

        }
         return head1;  
    }
    public ListNode reverseList(ListNode head)  //链表反转方法
    {
        ListNode headNode=new ListNode(0);
        headNode.next=head;
        ListNode current=head;
        ListNode temp;
        while(current.next!=null)
        {
            temp=current.next;
            current.next=temp.next;
            temp.next=headNode.next;
            headNode.next=temp;
        }
        return headNode.next;
    }
}

但是出现错误如下:
沃多克!!宝宝心塞啊!!
那么我们就对症下药,将链表反转寻找到第一个不一样的节点后,继续遍历A链表,查看A链表中与B链表中不一样的节点数num,然后,将原来保存好的链表头结点依次遍历num个数后返回该节点。还是出现以上错误。

百思不得其解,其实,我离成功只差一步了,哈哈哈!!!!可以想到反转链表计算其不一样的链表节点数,为啥子没想到,A链表-B链表=num,只要B链表遍历时的步数比A链表多num步,就可以依次同步遍历,直到寻找到相同的节点为止!!!
说了这么多废话,请看如下代码:
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) {
        int i=1,
            j=1;
         ListNode head1=headA;   //保存原链表的头节点
         ListNode head2=headB;   //保存原链表的头节点
         if(head1==null||head2==null)//其中一个链表为空都返回空
         {
             return null;
         }
         while(head1.next!=null)     //遍历A链表,计算总的节点数目
         {
             i++;
             head1=head1.next;
         }
          while(head2.next!=null)    //遍历B链表,计算总的节点数目
         {
             j++;
             head2=head2.next;
         }
         if(head1!=head2)
         {
             return null;  //最后一个节点不相等,则无交叉节点
         }
         int num=i-j;
         if(num>0)        //A链表数目多时,先遍历多出的节点数目
         {
             while(num>0)
             {
                 headA=headA.next;
                 num--;
             }
         }{         
             num=Math.abs(num); //B链表数目多时,遍历多出的节点数目
              while(num>0)
             {
                 headB=headB.next;
                 num--;
             }
         }
         while( (headA!= headB)&&(headA!=null)&&(headB!=null))   //依次遍历A,B链表,寻找交叉节点
         {
             headA=headA.next;    
             headB=headB.next;
         }
         return headA;

         }

    }
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode-160-Intersection of Two Linked Lists

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two...

Intersection of Two Linked Lists leetcode

Write a program to find the node at which the intersection of two singly linked lists begins. ...

Leetcode: Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. F...

LeetCode (160) Intersection of Two Linked Lists

题目Write a program to find the node at which the intersection of two singly linked lists begins. For...
  • fly_yr
  • fly_yr
  • 2015年11月13日 13:10
  • 447

【Leetcode】:Intersection of Two Linked Lists

题目链接:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 分析:题目就是求两个链表的的第一个交点,如果没有交点...

[LeetCode]problem 160. Intersection of Two Linked Lists

TAG求两个无环链表第一个相交节点 题目链接方法MD智障啊。真是不服不行。编程之美上有一个基础的问题,只判断是否相交;然后后续有个问题就是如果需要找第一个相交节点该怎么办?当时我随便一想,觉得应该没有...

【leetcode 两个链表的交集点】Intersection of Two Linked Lists

leetcode上最新的面试题目,求两个链表的交集,难度为easy,附个人理解解析以及题目作者的英文解析。...

Leetcode之Intersection of Two Linked Lists

leetcode之Intersection of Two Linked Lists分析,双指针

[LeetCode] Intersection of Two Linked Lists

Intersection of Two Linked Lists   Write a program to find the node at which the intersection ...

[LeetCode][160][Intersection of Two Linked Lists ]

题目链接:https://leetcode.com/problems/intersection-of-two-linked-lists/ 题目描述: Write a program to fin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Intersection of Two Linked Lists
举报原因:
原因补充:

(最多只允许输入30个字)