Old Wei wins the offer to take you to learn --- Brush title series (the first 36. The two common node of the list)

A first common junction 36. The two lists of

problem:

Two input lists, find their first common node. (Note that because the incoming data is a linked list, so an error of test data is displayed in other ways to ensure that incoming data is correct)

solve:

thought:

Common node mean values ​​are the same, we simply must first traverse a linked list first, and then the inside of value into a list. The second linked list is then traversed, if the value of which node in the list, the node is returned.

python code:

# -*- 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=[]
        p1=pHead1
        p2=pHead2
        
        while(p1!=None):
            list1.append(p1.val)
            p1=p1.next
        while(p2!=None):
            if(p2.val in list1):
                return p2
            else:
                p2=p2.next
Published 160 original articles · won praise 30 · views 70000 +

Guess you like

Origin blog.csdn.net/yixieling4397/article/details/105023025