题目:输入两个链表,找出它们的第一个公共结点。
思路:用python解决的话,相对比较简单,把链表1中的值依次放到list1,然后依次遍历链表2,如果遍历到的值在list1中,说明找到了第一个公共结点,return即可。当然边界情况需要考虑。
python代码如下:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
# write code here
list1 = []
list2 = []
node1 = pHead1
node2 = pHead2
while node1:
list.append(node1.val)
while node2:
if node2.val in list1:
return node2
else:
node2 = node2.next