Leetcode OJ 160. Intersection of Linked Lists

Source: Internet
Author: User

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

For example, the following, linked lists:

A:          a1→a2                                        c1→c2→c3                               B:     b1→b2→b3

Begin to intersect at node C1.

Notes:

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

This topic is to find out the intersection of two linked lists, how to solve it? If the two linked lists intersect, we move from the A-list to a distance of Alen, and the B lists move a distance blen, then they point to the same node C1. So how much is this distance?

We look at each list as two paragraphs, disjoint segments and intersecting sections. The intersecting section is the same for two linked list lengths, and the length of the list you don't want to make is different. If we calculate the length of the two linked lists separately, then calculate the difference f of their length, then move the distance F on the long list, and then start the traversal from the A-B list, and if they point to the same node, they intersect and return the intersecting points, and if they do not want to, they will traverse to the tail of the list. Then NULL is returned. The code is as follows:

1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {7 * val = x;8 * next = null;9  *     }Ten  * } One  */ A  Public classSolution { -      PublicListNode Getintersectionnode (ListNode heada, ListNode headb) { -ListNode P1 = Heada, p2 =headb; the         intLen1 = 0, len2 = 0; -          while(P1! =NULL) {//to find the length of a chain list -P1 =P1.next; -len1++; +         } -          while(P2! =NULL) {//find the length of List B +P2 =P2.next; Alen2++; at         } -P1 =Heada; -P2 =headb; -         if(Len1 > Len2) {//calculates the difference between the length of the list and moves backwards on the long list |len1-len2| -              for(inti = 0;i < len1-len2; i++) { -P1 =P1.next; in             } -}Else { to              for(inti = 0;i < len2-len1; i++) { +P2 =P2.next; -             } the         } *          while(P1! = p2) {//traverse the linked list A and List B backwards, find the intersecting node, and if you traverse to the end, return null $P1 =P1.next;Panax NotoginsengP2 =P2.next; -         } the         returnP1; +     } A}

Leetcode OJ 160. Intersection of Linked Lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.