-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
两个链表的第一个公共结点.py
38 lines (32 loc) · 1.07 KB
/
两个链表的第一个公共结点.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'''
输入两个链表,找出它们的第一个公共结点。
'''
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
nLength1 = self.GetListLength(pHead1)
nLength2 = self.GetListLength(pHead2)
nLengthDiff = abs(nLength1 - nLength2)
if nLength1 > nLength2:
pListHeadLong = pHead1
pListHeadShort = pHead2
else:
pListHeadLong = pHead2
pListHeadShort = pHead1
for i in range(nLengthDiff):
pListHeadLong = pListHeadLong.next
while pListHeadLong != None and pListHeadShort != None and pListHeadLong != pListHeadShort:
pListHeadLong = pListHeadLong.next
pListHeadShort = pListHeadShort.next
pFirstCommonNode = pListHeadLong
return pFirstCommonNode
def GetListLength(self, pHead):
nLength = 0
while pHead != None:
pHead = pHead.next
nLength += 1
return nLength