奇数结点升序偶数结点降序的单链表排序(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 ...
随机推荐
- X Samara Regional Intercollegiate Programming Contest DIV2
http://codeforces.com/gym/101341 其实我觉得这份题很不错的,虽然是div2,但是感觉对我挺有帮助(我比较垃圾0.0),还没补完(做的时候一直蒙逼,要补很多题)先写一点点 ...
- Storm概念学习系列之并行度与如何提高storm的并行度
不多说,直接上干货! 对于storm来说,并行度的概念非常重要!大家一定要好好理解和消化. storm的并行度,可以简单的理解为多线程. 如何提高storm的并行度? storm程序主要由spout和 ...
- 密码强度的正则表达式(JavaScript)总结
简言 本文给出了两个密码强度的正则表达式方案,一个简单,一个更复杂和安全.并分别给出了两个方案的解析和测试程序.一般大家可以根据自己的项目的实际需要,自行定义自己的密码正则约定. 前言 用户注册时,都 ...
- react中constructor和super()以及super(props)的区别。
react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...
- ZOJ 3494 BCD Code (数位DP,AC自动机)
题意: 将一个整数表示成4个bit的bcd码就成了一个01串,如果该串中出现了部分病毒串,则是危险的.给出n个病毒串(n<=100,长度<21),问区间[L,R]中有几个数字是不含病毒串的 ...
- 抓取GridView "编辑"模式下,TextBox修改后的数值
[FAQ]抓取GridView "编辑"模式下,TextBox修改后的数值 -- ASP.NET专题实务「上集」Ch.10 抓取GridView "编辑"模式下 ...
- 让您的Eclipse具有千变万化的外观
大家每天用Eclipse做Java开发,是否厌倦了Eclipse千篇一律的白色背景呢? 看看Jerry这几种不同风格的Eclipse外观,是不是有耳目一新的感觉?如何做到的? 需要给Eclipse安装 ...
- 如何用JavaScript实现2+2=5?
我大学毕业找工作时,经常做一些稀奇古怪的面试题.这不,给大家分享一道整蛊的面试题,它其实不能算一道正式的面试题,大家可以用它来捉弄你们那些程序员朋友. 题目:如何用JavaScript实现2+2=5? ...
- Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)
E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- UVA11090 Going in Cycle (二分+判负环)
二分法+spfa判负环.如果存在一个环sum(wi)<k*x,i=0,1,2...,k,那么每条边减去x以后会形成负环.因此可用spfa来判负环. 一般spfa判负环dfs最快,用stack次之 ...