Intersection of Two Linked Lists(java)

eg:        a1 → a2
                
                     c1 → c2 → c3 或者直接a1 → b1
               b1 → b2 → b3求公共链表c1.

常规的指针分裂法,复制法,偏移法。
 1 public class Solution {
 2     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
 3         if(headA==null||headB==null) return null;
 4         int lengthA=0;
 5         int lengthB=0;
 6         ListNode currentA=headA;
 7         ListNode currentB=headB;
 8         while(currentA.next!=null)
 9         {
10             currentA=currentA.next;
11             lengthA++;
12         }
13          while(currentB.next!=null)
14         {
15              currentB=currentB.next;
16             lengthB++;
17         }
18         ListNode fast=lengthA>lengthB?headA:headB;
19         ListNode slow=lengthA>lengthB?headB:headA;
20         for(int i=0;i<Math.abs(lengthA-lengthB);i++)
21         {
22             fast=fast.next;
23         }
24
25         while(fast!=null)
26         {
27             if(fast==slow)
28             return fast;
29             else
30             {
31                 fast=fast.next;
32                 slow=slow.next;
33             }
34         }
35         return null;
36     }
37 }
时间: 2024-11-08 21:25:18

Intersection of Two Linked Lists(java)的相关文章

leetcode 160. Intersection of Two Linked Lists --------- java

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 i

160. Intersection of Two Linked Lists java solutions

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 i

leetcode_160题——Intersection of Two Linked Lists(线性表,哈希表)

Intersection of Two Linked Lists Total Accepted: 28581 Total Submissions: 100989My Submissions Question Solution Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists

leetCode 160. Intersection of Two Linked Lists 链表

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

[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 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 i

【leetcode】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:

leetcode Intersection of Two Linked Lists python

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

[LintCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. Notice If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. Y

160. Intersection of Two Linked Lists(js)

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: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA