python实现合并链表_python:16.合并两个排序的链表
生活随笔
收集整理的這篇文章主要介紹了
python实现合并链表_python:16.合并两个排序的链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
輸入兩個單調遞增的鏈表,輸出兩個鏈表合成后的鏈表,當然我們需要合成后的鏈表滿足單調不減規則。
解析
2. 構建兩個頭結點
# -*- coding:utf-8 -*- class ListNode:def __init__(self, x):self.val = xself.next = None class Solution:# 返回合并后列表def Merge(self, pHead1, pHead2):# write code hereres = head = ListNode(0)while pHead1 and pHead2:if pHead1.val < pHead2.val:head.next = pHead1pHead1 = pHead1.nextelse:head.next = pHead2pHead2 = pHead2.nexthead = head.nextif pHead1:head.next = pHead1if pHead2:head.next = pHead2return res.next總結
以上是生活随笔為你收集整理的python实现合并链表_python:16.合并两个排序的链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 如何判断h5页面是否加载
- 下一篇: python json转换字典_Pyth