160-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.

题目很简单就是找两个交叉链表的交点。如果没有就返回null

正常的思路是, 先获取到两个链表的长度 l1,l2 . 然后将长的链表 先移动 |l1 -l2|长度, 然后在一起移动如果有相等的节点 那就是交点
下面是我的代码...一次写的比较丑。

/**
 * 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 lenA = getListLen(headA);
        int lenB = getListLen(headB);
        
        
        ListNode correct = (lenA > lenB) ? headA : headB;
        ListNode other = (correct == headA) ? headB : headA;
        
        int correctLen = Math.abs(lenA - lenB);
        while(correctLen != 0){
            correct = correct.next;
            correctLen--;
        }
        
        while(correct != null && other != null){
            if(correct == other){
                return correct;
            }
            correct = correct.next;
            other = other.next;
        }
        return null;
    }
    
    private int getListLen(ListNode head){
        int i = 0;
        ListNode temp = head;
        while(temp != null){
            i++;
            temp = temp.next;
        }
        return i;
    }
}

有趣的是,有大神提出了 不需要获取长度的算法 让我膝盖一跪。
简单的来说或就是先同时遍历两个链表:
一种情况是 长度为 a 和 b (a < b)
当 a 到头的时候 a 换成 b链表 和b同时开始。当b换到a链表的时候 a又在b链表上移动了 (b - a)的距离。
所以这个时候 就相等于前面的 “先移动 |l1 -l2|长度” 的动作了后续就比较又没交叉节点就好了。

@hpplayer said in Java solution without knowing the difference in len!:

I found most solutions here preprocess linkedlists to get the difference in len.
Actually we don't care about the "value" of difference, we just want to make sure two pointers reach the intersection node at the same time.

We can use two iterations to do that. In the first iteration, we will reset the pointer of one linkedlist to the head of another linkedlist after it reaches the tail node. In the second iteration, we will move two pointers until they points to the same node. Our operations in first iteration will help us counteract the difference. So if two linkedlist intersects, the meeting point in second iteration must be the intersection point. If the two linked lists have no intersection at all, then the meeting pointer in second iteration must be the tail node of both lists, which is null

Below is my commented Java code:

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    //boundary check
    if(headA == null || headB == null) return null;
    
    ListNode a = headA;
    ListNode b = headB;
    
    //if a & b have different len, then we will stop the loop after second iteration
    while( a != b){
      //for the end of first iteration, we just reset the pointer to the head of another linkedlist
        a = a == null? headB : a.next;
        b = b == null? headA : b.next;    
    }
    
    return a;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、 北郭北村地处S市的郊区,是历史悠久的老小区,且每幢楼只有三层,六户人家,里面居住的大部分都是上了年纪的老人,...
    林白勺阅读 6,266评论 10 37
  • 老Z,是我一姐們的老爹。快八十歲了。一生坎坷,傳奇。他,也是我至今為止所見過的自戀指數最高,高到爆棚再爆棚,然後把...
    MABEL梅阅读 1,313评论 0 0
  • 1 如是灭度无量无数无边众生,实无众生得灭度者。 2 若菩萨有我相,人相,众生相,寿者相,即非菩萨。 3 菩萨于法...
    薄荷草2016阅读 3,746评论 0 0