题目

一个链表,奇数结点升序,偶数结点降序,要求变成一个全升序的链表。

例如:1->8->2->7->3->6->4->5,变为1->2->3->4->5->6->7->8

解析

按照以下步骤处理:

  1. 按照奇偶位拆分为两个链表
  2. 反转偶数结点构成的链表
  3. 合并两个递增链表

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实现)的更多相关文章

  1. C语言链表:逆序建立单链表

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<malloc.h> #define LEN sizeof( ...

  2. 算法导论--装备线调度(升序&amp;&amp;降序输出)

    题意就先不用讲了吧,感觉自己还没有掌握核心的东西. //心得 //如何保持路径,递归的实现 #include<iostream> #include<cstdio> #inclu ...

  3. DataTable进行排序Asc升序,Desc降序

    DataTable dt = new DataTable(); DataView dv = dt.DefaultView; dv.Sort = "XXX Asc"; dt=dv.T ...

  4. 面试题26:合并k个排好序的单链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. c ...

  5. mysql中的升序和降序以及一个字段升序和一个字段降序

    mySql中,升序为asc,降序为desc.例如: 升序:select   *  from  表名 order by  表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select   ...

  6. SQL-ORDER BY 多字段排序(升序、降序)

    ORDER BY _column1, _column2; /* _column1升序,_column2升序 */   ORDER BY _column1, _column2 DESC; /* _col ...

  7. mysql 升序降序

    默认不指定,order by 按照升序排列. asc:升序 desc:降序

  8. mysql中一个字段升序,另一个字段降序

    mySql中,升序为asc,降序为desc.例如: 升序:select   *  from  表名 order by  表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select   ...

  9. sql中使一个字段升序,一个字段降序

    ORDER BY _column1, _column2; /* _column1升序,_column2升序 */ ORDER BY _column1, _column2 DESC; /* _colum ...

随机推荐

  1. ADC5513

    一 C5513 u32 ADC5513_GetValue(void){  u32 ADValue,i;  bool data_bit = false;   C5513_SCK=0;  C5513_CS ...

  2. hbase按照时间戳删除记录

    1.按照时间戳范围查询记录 echo "scan 'event_log', { COLUMN => 'cf:sid', TIMERANGE => [1466265600272, ...

  3. [摘录]全面学习GFW

    转载自:https://cokebar.info/archives/253 GFW会是一个长期的存在.要学会与之共存,必须先了解GFW是什么.做为局外人,学习GFW有六个角度.渐进的来看分别是: 首先 ...

  4. Php—AJAX跨域问题

    <?php /** * ajax proxy * ajax跨域解决办法 * @author  suconghou <suconghou@126.com> * @version v1. ...

  5. Java中的日志框架

    日志框架的介绍和使用 常见的日志框架:JUL(Java.util.logging),JCL(jakarta commons logging),SLF4J,jboss-logging,Log4j,Log ...

  6. SPEC CPU 2006编译perl 出错:undefined reference to `pow'

    问题来源: 编译spec2006时,出现如下错误: cc -L/home/yrtan/benchmark/2006/CPU2006v1.0.1/tools/output/lib -L/usr/loca ...

  7. mysql-练级查询

    mysql的链接查询中主要有五大类链接查询 1.内连接查询 1.1:等值链接查询:指使用等号"="比较两个表的连接列的值,相当于两表执行笛卡尔后,取两表连结列值相等的记录. SEL ...

  8. python基础教程总结2——字符串

    1.基本操作 序列操作:索引,分片,乘法,判断成员资格,长度,最值...... 注:字符串不可变,分片赋值不合法 2.字符串格式化 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格 ...

  9. CornerStone使用跳坑总结

    Cornerstone是专门为Mac用户设计的Subversion(SVN)的控制,使版本控制更加透明.cornerstone根Xcode相比,能够更好的忽略文件,所以除了项目经理第一次初始化项目的时 ...

  10. Mysql 8.0 新特性

    转载:https://www.jianshu.com/p/be29467c2b0c