奇数结点升序偶数结点降序的单链表排序(Python实现)
题目
一个链表,奇数结点升序,偶数结点降序,要求变成一个全升序的链表。
例如:1->8->2->7->3->6->4->5,变为1->2->3->4->5->6->7->8
解析
按照以下步骤处理:
- 按照奇偶位拆分为两个链表
- 反转偶数结点构成的链表
- 合并两个递增链表
Python实现
# -*- coding:utf-8 -*-
class Node(object):
def __init__(self, val=None, next=None):
self.val = val
self.next = next
def init_list(l):
"""创建不带头结点的单链表"""
head = Node()
tail = head
for val in l:
tail.next = Node(val)
tail = tail.next
tail.next = None
return head.next
def split_list(head):
"""按照奇偶位拆分为两个链表"""
head1 = head2 = None
cur1 = cur2 = None
count = 1
while head:
if count % 2 == 1:
if cur1:
cur1.next = head
cur1 = cur1.next
else:
cur1 = head1 = head
else:
if cur2:
cur2.next = head
cur2 = cur2.next
else:
cur2 = head2 = head
head = head.next
count += 1
cur1.next = None
cur2.next = None
return head1, head2
def reverse_list(head):
"""反转链表"""
if not head or not head.next:
return head
pre = next = None
while head:
next = head.next
head.next = pre
pre = head
head = next
return pre
def merge_list(head1, head2):
"""合并两个递增链表"""
head = Node() # 设置一个临时结点
tail = head
while head1 and head2:
if head1.val <= head2.val:
tail.next = head1
head1 = head1.next
else:
tail.next = head2
head2 = head2.next
tail = tail.next
# 合并剩余结点
if head1:
tail.next = head1
if head2:
tail.next = head2
return head.next
def visit_list(head):
while head:
print(head.val)
head = head.next
if __name__ == '__main__':
head = init_list([1, 8, 2, 7, 3, 6, 4, 5]) # 创建一个不带头结点的单链表:1->8->2->7->3->6->4->5
head1, head2 = split_list(head) # 1.按照奇偶位拆分为两个链表
head2 = reverse_list(head2) # 2.反转偶数结点构成的链表
head = merge_list(head1, head2) # 3.合并两个递增链表
visit_list(head) # 遍历链表
奇数结点升序偶数结点降序的单链表排序(Python实现)的更多相关文章
- C语言链表:逆序建立单链表
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<malloc.h> #define LEN sizeof( ...
- 算法导论--装备线调度(升序&&降序输出)
题意就先不用讲了吧,感觉自己还没有掌握核心的东西. //心得 //如何保持路径,递归的实现 #include<iostream> #include<cstdio> #inclu ...
- DataTable进行排序Asc升序,Desc降序
DataTable dt = new DataTable(); DataView dv = dt.DefaultView; dv.Sort = "XXX Asc"; dt=dv.T ...
- 面试题26:合并k个排好序的单链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. c ...
- mysql中的升序和降序以及一个字段升序和一个字段降序
mySql中,升序为asc,降序为desc.例如: 升序:select * from 表名 order by 表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select ...
- SQL-ORDER BY 多字段排序(升序、降序)
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */ ORDER BY _column1, _column2 DESC; /* _col ...
- mysql 升序降序
默认不指定,order by 按照升序排列. asc:升序 desc:降序
- mysql中一个字段升序,另一个字段降序
mySql中,升序为asc,降序为desc.例如: 升序:select * from 表名 order by 表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select ...
- sql中使一个字段升序,一个字段降序
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */ ORDER BY _column1, _column2 DESC; /* _colum ...
随机推荐
- (转)useradd用户,组管理案例
原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 示例:要求oldbo ...
- 机器学习框架ML.NET学习笔记【5】多元分类之手写数字识别(续)
一.概述 上一篇文章我们利用ML.NET的多元分类算法实现了一个手写数字识别的例子,这个例子存在一个问题,就是输入的数据是预处理过的,很不直观,这次我们要直接通过图片来进行学习和判断.思路很简单,就是 ...
- CentOS7.2安装MySql5.7并开启远程连接授权
1.安装mysql5.7 CentOS 7之后的版本yum的默认源中使用MariaDB替代原先MySQL,因此安装方式较为以往有一些改变: 下载mysql的源 wget http://dev.mysq ...
- python中的getcwd
Help on built-in function getcwd in module posix: getcwd(...) getcwd() -> path Return a ...
- Android商城开发系列(二)——App启动欢迎页面制作
商城APP一般都会在应用启动时有一个欢迎界面,下面我们来实现一个最简单的欢迎页开发:就是打开商城App,先出现欢迎界面,停留几秒钟,自动进入应用程序的主界面. 首先先定义WelcomeActivity ...
- 洛谷 P2663 越越的组队
题目描述 班级要组织一场综合能力竞赛,全班同学(N个,N是偶数)分成两队互相竞争.老师找到了越越并给了越越一张全班同学综合能力测试的成绩,要求他从全班同学中选出一半(他自己也可能被选),并要求这些同学 ...
- “IIS7.5无法写入配置文件web.config”的解决方案
原因 用了非NTSF(如:FAT32或exFAT分区格式,导致无法设置安全选项,从而导致无法自动写入文件). 解决方案 先在默认网站配置好,然后把web.config复制到目标网站的根目录,这样就等于 ...
- MovieReview—Kingsman THE SECRET SERVICE(王牌特工之特工学院)
Brain Storming Mr. Valentine's Day see excess human beings as the Earth's virus and try to e ...
- JAVA小游戏之两个物体碰撞产生的碰撞检测
首先必须了解两个物体,在移动时,会有怎样的效果,比如沪我们小时候耍过的坦克大战.看起来很简单,但是写起代码来,复杂的要多: 下面举个例子: // 构造一个新的 Rectangle,其左上角的坐标为 ( ...
- XManager 远程连接Netbackup图形用户界面
XManager远程连接Netbackup图形用户界面 目标: 在自己的Windows桌面打开Linux的Netbackup图形用户界面 工具: Windows: Xmanager,Xshell, ...