题目

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

例如: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. Spark Mllib里如何对决策树二元分类和决策树多元分类的分类数目numClasses控制(图文详解)

    不多说,直接上干货! 决策树二元分类的分类数目numClasses控制 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第13章 使用决策树二元分类算法来预测分类Stumble ...

  2. 获取spring里的bean

    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring. ...

  3. 关于C#解析shp文件

    最近在做项目时,要求可以上传shp文件到指定的地图中,地图开发使用的arcgisapi,网上找了好多解析shp文件的js,但都不是太理想,直到群里的小伙伴提到Gdal 首先,到GDAL官网下载自己使用 ...

  4. MVC下载文件方式 包括网络地址文件

    MVC下载文件方式 方式一: public FileStreamResult DownFile(string filePath, string fileName){      string absol ...

  5. webpack.config.js====output出口文件的配置

    output: { filename: './js/[name].[hash:8].js', /* * filename:在使用webpack-dev-server模式时,如果要使用hash,是不可以 ...

  6. springboot项目实现批量新增功能

    这个困扰我一整天东西,终于解决了. 首先是mybatis中的批量新增sql语句. 注意:这里我给的是我需要新增的字段,你们改成你们需要的字段. <insert id="insertBa ...

  7. JavaBean+jsp开发模式 --结合form表单 实例

    1.创建form表单 <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

  8. centos 离线安装 mysql 5.7

    1 . 安装新版mysql前,需将系统自带的mariadb-lib卸载. rpm -qa|grep mariadb mariadb-libs--.el7.centos.x86_64 rpm -e -- ...

  9. idea2018.1.2 激活

    https://www.cnblogs.com/ycjcham/p/8724451.html

  10. 【extjs6学习笔记】1.3 初始:根据模板创建项目

    使用sencha创建应用 命令说明:sencha -sdk /path/to/sdk generate app -s /your/templates/path/ MyApp /path/to/myap ...