音乐自助建站,特色的企业网站建设,网站维护托管,快速建站费用原创#xff1a; 老表 简说Python 今日问题 #xff1a;翻转链表k个相邻结点目标#xff1a;写一段程序#xff0c;合并两个有序链表例如#xff1a;输入- 1-2-3输入- 2-5-6-8输出- 1-2-2-3-5-6- 老表 简说Python 今日问题 翻转链表k个相邻结点目标写一段程序合并两个有序链表例如输入- 1-2-3输入- 2-5-6-8输出- 1-2-2-3-5-6-8Goal: write a program to merge two ordered listsFor example:Input - 1 - 2 - 3Input - 2 - 5 - 6 - 8Output - 1 - 2 - 2 - 3 - 5 - 6 - 8解题准备首先我们写好链表的基本操作在a_0_base.py文件中目前包含对链表的定义类初始化函数遍历函数。(带头结点)# -*- coding: utf-8 -*-author 老表date 2019-10-19个人微信公众号 : 简说Python# 链表数据结构定义class ListNode: def __init__(self, x): self.data x self.next Noneclass ListOperation: a 1 # 根据链表数据初始化链表 staticmethod def init_list(n_list): # 初始化一个头指针 head ListNode(head) cur head for i in n_list: node ListNode(i) cur.next node cur node # 相当于 cur cur.next后移 return head # 根据链表数据初始化一个有环的链表 staticmethod def init_ring_list(n_list): # 初始化一个头指针 head ListNode(head) cur head for i in n_list: node ListNode(i) cur.next node cur node # 相当于 cur cur.next后移 cur.next head.next.next.next.next # 随意的让最后一个结点指向第四个结点 return head # 遍历链表 staticmethod def ergodic_list(head): # print(head.data) cur head.next while cur: print(cur.data) cur cur.next # 获取链表长度 staticmethod def get_len_list(head): cur head.next len_list 0 while cur: len_list len_list 1 cur cur.next return len_list解题开始程序前需提前导入前面写好的链表基本操作包和结点数据结构在Linked_list的a_0_base.py中。from Linked_list.a_0_base import ListOperation解题思路Method One : 遍历插入法核心思想先确定合并后链表的头结点(表头小的)然后将两个链表一起遍历将表头数值大的链表结点插入链表表头结点数值小的链表。时间复杂度O(N)空间复杂度O(1)功能代码def merge_two_ordered_list_one(head1, head2): if not head1.next: # 空链表 return head2 # 返回头结点 if not head2.next: # 空链表 return head1 # 返回头结点 # 合并第一步判断谁的开始结点小确定返回链表 cur_node1 head1.next # 记录链表一当前结点 cur_node2 head2.next # 记录链表二当前结点 if cur_node1.data cur_node2.data: # 判断表头数值大小 # 链表1的表头小则将链表2结点插入链表1 head head1 # 记录合并后链表头结点 cur_node cur_node1 # 记录合并后链表当前结点 cur_node1 cur_node1.next # 链表1当前结点后移遍历 else: # 链表2的表头小则将链表1结点插入链表2 head head2 # 记录合并后链表头结点 cur_node cur_node2 # 记录合并后链表当前结点 cur_node2 cur_node2.next # 链表2当前结点后移遍历 # 合并第二步遍历比较结点大小依次插入小的结点 while cur_node1 and cur_node2: # 开始遍历插入 if cur_node1.data cur_node2.data: # 链表1的结点小 cur_node.next cur_node1 # 将链表1该结点插入合并后的链表(准确来说只是追加) cur_node cur_node1 # 记录合并后的链表的当前结点 cur_node1 cur_node1.next # 链表1当前结点后移继续遍历 else: # 链表2的结点小 cur_node.next cur_node2 # 将链表2该结点插入合并后的链表(准确来说只是追加) cur_node cur_node2 # 记录合并后的链表的当前结点 cur_node2 cur_node2.next # 链表2当前结点后移继续遍历 # 遍历结束将未遍历完的链表剩余结点加入到合并后的链表结尾 if cur_node1: # 链表1还未遍历完 cur_node.next cur_node1 if cur_node2: # 链表2还未遍历完 cur_node.next cur_node2 return head # 返回头结点测试代码# 当然也许还有别的方法比如建一个辅助的链表# 欢迎你说出你的想法# 程序入口测试函数功能if __name__ __main__: list_data1 [1, 2, 3] # 链表1初始化数据 list_data2 [2, 4, 5, 6] # 链表2初始化数据 head1 ListOperation.init_list(list_data1) # 初始化链表1带头结点 head2 ListOperation.init_list(list_data2) # 初始化链表2带头结点 ListOperation.ergodic_list(head1) # 未操作前遍历打印链表1 print(___________________________) ListOperation.ergodic_list(head2) # 未操作前遍历打印链表2 head merge_two_ordered_list_one(head1, head2) # 测试方法一 print(---------------------) ListOperation.ergodic_list(head) # 操作后遍历打印链表本文代码思路部分来自书籍《Python程序员面试宝典》书中部分代码有问题或未提供代码文中已经修改过了并添加上了丰厚的注释方便大家学习后面我会把所有内容开源放到Github上包括代码思路算法图解(手绘或者电脑画)时间充裕的话会录制视频。希望大家多多支持。最后我自己是一名从事了多年开发的Python老程序员辞职目前在做自己的Python私人定制课程今年年初我花了一个月整理了一份最适合2019年学习的Python学习干货可以送给每一位喜欢Python的小伙伴想要获取的可以关注我的头条号并在后台私信我教程即可免费获取。