LeetCode刷题EASY篇 Intersection of Two Linked Lists

题目

 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.

我的尝试

两个指针方法,发现有问题,我的代码在等长情况没有问题,但是长度不一样就不行了。

/**
 * 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) {
        while(headA!=null&&headB!=null){
            if(headA.val==headB.val){
                return headA;
            }
            headA=headA.next;
            headB=headB.next;  
        }
        return null;
    }
}

可以考虑利用hashmap存储一个链表的数据,然后循环第二个链表,判断是否有相同元素.测试通过

/**
 * 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) {
        Set set=new HashSet();
        while(headA!=null){
            set.add(headA.val); 
            headA=headA.next;
         }
        while(headB!=null){
            if(set.contains(headB.val)){
                return headB;
            }
            headB=headB.next;
        }
        return null;
    }
}

虽然测试通过,但是有问题,不要保存val在set,因为可能不相交,但是val相同,所以应该存储节点地址,修改如下:

/**
 * 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) {
        Set set=new HashSet();
        while(headA!=null){
            set.add(headA); 
            headA=headA.next;
         }
        while(headB!=null){
            if(set.contains(headB)){
                return headB;
            }
            headB=headB.next;
        }
        return null;
    }
}

其他解法

除了刚才的hashset方法,继续研究两个指针的方法,因为这是个重要的思路。看了其他高手的思路,发现如果长度不一样,调整到同一个起点就ok。于是修改我的两个指针代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    
    public int getLength(ListNode head){
        int length=0;
        while(head!=null){
            length++;
            head=head.next;
        }
        return length;
    }
    
    
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
       int lengthA=getLength(headA);
       int lengthB=getLength(headB);
       while(lengthA>lengthB){
           headA=headA.next;
           lengthA--;
       }
       while(lengthB>lengthA){
           headB=headB.next;
           lengthB--;
       }
        while(headA!=null&&headB!=null){
            if(headA==headB){
                return headA;  
            }
            headA=headA.next;
            headB=headB.next;
        }
        return headA;
    }
}

猜你喜欢

转载自blog.csdn.net/hanruikai/article/details/84937195
今日推荐