【题目】
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。
【代码实现】
#python3.5
class Node:
def __init__(self, val=None):
self.val = val
self.next = None
def printCommonPart(head1, head2):
if head1 == None or head2 == None:
return
print("Common Part:", end=' ')
while head1 != None and head2 != None:
if head1.val > head2.val:
head2 = head2.next
elif head1.val < head2.val:
head1 = head1.next
else:
print(head1.val, end=' ')
head1 = head1.next
head2 = head2.next
print()