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

解法思路:

1. 先遍历List A 和 List B,  分别得到List A和List B的长度

2. 用长的list 减去短的List,e.g. x = |A - B|, 然后遍历长的List到X

3. 分别用2个指针指向2个List, 再进行判断即可

class Solution:
# @param two ListNodes
# @return the intersected ListNode
  def getIntersectionNode(self, headA, headB):
    temp = None

    temp = headA
    a = 0
    while temp != None:
      a += 1
      temp = temp.next

    b = 0
    temp = headB
    while temp != None:
      b += 1
      temp = temp.next

    x = 0
    temp1 = None
    if a > b:
      x = a - b
      temp = headA
      for i in range(x):
        temp = temp.next
      temp1 = headB
    else:
      x = b - a
      temp = headB
      for i in range(x):
        temp = temp.next
      temp1 = headA

    flag = False
    while temp != None and temp1 != None:
      if temp == temp1:
        flag = True
        break
      else:
        flag = False
        temp = temp.next
        temp1 = temp1.next

    if flag:
      return temp
    else:
      return None

时间: 2024-08-25 07:48:57

3. Intersection of Two Linked Lists的相关文章

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

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

[C++]LeetCode: 60 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

[LeetCode160]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 h