# (LeetCode)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.

Credits:
Special thanks to @stellari for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None

class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
listA= []
listB= []
if headA == None or headB == None:
return None
while 1:
if headA == None:
break
listA.append(headA.val)
headA = headA.next

while 1:
if headB == None:
break
listB.append(headB.val)
headB = headB.next

if listA[-1] != listB[-1]:
return None
if len(listA) < len(listB):
minLen = len(listA)
else:
minLen = len(listB)
for i in range(1, minLen + 1):
if listA[-i] != listB[-i]:
return ListNode(listA[-i + 1])
if i == minLen:
return ListNode(listA[-i])



#### 【leetcode 两个链表的交集点】Intersection of Two Linked Lists

2014-11-28 00:15:09

#### 【LeetCode-面试算法经典-Java实现】【160-Intersection of Two Linked Lists（单链表的交集）】

2015-08-22 06:45:36

#### LeetCode 160 ：Intersection of Two Linked Lists

2015-05-14 22:06:04

#### leetcode 160: Intersection of Two Linked Lists

2014-12-30 03:17:29

#### [leetcode]Intersection of Two Linked Lists　——　熟悉python

2014-12-11 11:41:03

#### LeetCode 160 — Intersection of Two Linked Lists（C++ Python）

2014-12-02 20:59:16

#### 160. Intersection of Two Linked Lists（python）

2016-09-18 21:38:02

#### leetcode Intersection of Two Linked Lists 两链表是否相交

2015-08-07 14:42:40

#### LeetCode--Intersection of Two Linked Lists （两个链表的交点）Python

2017-12-01 15:51:32

#### LeetCode_intersection-of-two-linked-lists

2015-09-14 10:16:22