合并两个排序的链表[剑指offer]之python实现

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

题目链接

开辟新的链表空间:

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        # write code here
        if pHead1==None and pHead2==None:
            return None
        elif pHead1!=None and pHead2==None:
            return pHead1
        elif pHead1==None and pHead2!=None:
            return pHead2
        else:
            p1=pHead1
            p2=pHead2
            if p1.valelse:
                newHead=p2
                p2=p2.next
            p=newHead
            while p1!=None and p2!=None:
                if p1.valelse:
                    p.next=p2
                    p=p.next
                    p2=p2.next
            if p1!=None:
                while p1!=None:
                    p.next=p1
                    p1=p1.next
                    p=p.next
            if p2!=None:
                while p2!=None:
                    p.next=p2
                    p2=p2.next
                    p=p.next
            return newHead

在原链表的基础上进行合并,写的都wrong了///

你可能感兴趣的:(Python,学习,算法练习)