05对链表进行重新排序★★★★

题目描述(难度:中等)

给定链表 L0->L1->L2…Ln-1->Ln,把链表重新排序为 L0->Ln->L1->Ln-1->L2-> Ln-2…。(来源:腾讯)
要求:
(1)在原来链表的基础上进行排序,即不能申请新的结点;
(2)只能修改结点的 next 域,不能修改数据域。

单链表结构

在这里插入图片描述

class LNode:
    def __init__(self):
        self.data = None # 数据域
        self.next = None # 指针域
  • 由于单链表中每个结点的地址都存储在其前驱结点的指针域中,因此,对单链表中任何一个结点的访问只能从链表的头指针开始进行遍历。
  • 注意:在修改结点指针域的时候,记录下后继结点的地址,否则会丢失后继结点。

方法一

在这里插入图片描述

# 找出链表Head的中间结点,把链表从中间断成两个子链表
def FindMiddleNode(head):
    if head is None or head.next is None:
        return head
    fast = head  # 遍历链表的时候每次向前走两步
    slow = head  # 遍历链表的时候每次向前走一步
    slowPre = head
    # 当fast到链表尾时,slow恰好指向链表的中间结点
    while fast is not None and fast.next is not None:
        slowPre = slow
        slow = slow.next
        fast = fast.next.next
    # 把链表断开成两个独立的子链表
    slowPre.next = None
    return slow

# 对不带头结点的单链表逆序
def Reverse(head):
    if head == None or head.next == None:
        return head

    pre = head  # 前驱结点
    cur = head.next  # 当前结点
    next = cur.next  # 后继结点
    pre.next = None
    # 使当前遍历到的结点cur指向其前驱结点
    while cur is not None:
        next = cur.next
        cur.next = pre
        pre = cur
        cur = cur.next
        cur = next
    return pre


# 对链表进行排序
def Reorder(head):
    if head == None or head.next == None or head.next.next == None:
        return
    # 前半部分链表第一个结点
    cur1 = head.next
    mid = FindMiddleNode(head.next)
    # 后半部分链表逆序后的第一个结点
    cur2 = Reverse(mid)
    tmp = None
    # 合并两个链表
    while cur1.next is not None:
        tmp = cur1.next
        cur1.next = cur2
        cur1 = tmp
        tmp = cur2.next
        cur2.next = cur1
        cur2 = tmp
    cur1.next = cur2

复杂度分析

  • 时间复杂度为 O ( n ) O(n) O(n)
  • 空间复杂度为 O ( 1 ) O(1) O(1)

主函数

if __name__ == "__main__":
    head = LNode()
    head.next = None
    tmp = None
    cur = head
    # 构造第一个链表
    link = [1, 2, 3, 4, 5]
    for i in link:
        tmp = LNode()
        tmp.data = i
        tmp.next = None
        cur.next = tmp
        cur = tmp

    print("排序前:", end='')
    cur = head.next
    while cur != None:
        print(cur.data, '', end='')
        cur = cur.next
    Reorder(head)
    print("\n排序后:", end='')
    cur = head.next
    while cur != None:
        print(cur.data, '', end='')
        cur = cur.next

参考文献

[1] 何海涛. 剑指Offer:名企面试官精讲典型编程题(第2版). 北京: 电子工业出版社,2017
[2] 张波,楚秦. Python程序员面试算法宝典. 北京:机械工业出版社,2018

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是C语言实现对链表进行冒泡排序的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 定义链表节点创建函数 struct ListNode* createNode(int val) { struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; return node; } // 定义链表插入函数 void insertNode(struct ListNode **head, int val) { struct ListNode *node = createNode(val); if (*head == NULL) { *head = node; } else { struct ListNode *cur = *head; while (cur->next != NULL) { cur = cur->next; } cur->next = node; } } // 定义链表打印函数 void printList(struct ListNode *head) { struct ListNode *cur = head; while (cur != NULL) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } // 定义链表冒泡排序函数 void bubbleSortList(struct ListNode *head) { if (head == NULL || head->next == NULL) { return; } struct ListNode *cur = head; while (cur != NULL) { struct ListNode *prev = NULL; struct ListNode *next = cur->next; while (next != NULL) { if (cur->val > next->val) { if (prev != NULL) { prev->next = next; } else { head = next; } cur->next = next->next; next->next = cur; prev = next; next = cur->next; } else { prev = cur; cur = next; next = next->next; } } cur = head; } } int main() { struct ListNode *head = NULL; insertNode(&head, 3); insertNode(&head, 1); insertNode(&head, 4); insertNode(&head, 2); printf("Before sort: "); printList(head); bubbleSortList(head); printf("After sort: "); printList(head); return 0; } ``` 以上代码中,我们首先定义了链表节点的结构体,并实现了链表节点的创建、插入和打印函数。然后我们定义了链表冒泡排序的函数,具体实现如下: 1. 如果链表为空或只有一个节点,则直接返回。 2. 从头节点开始遍历链表,对于每个节点cur,从cur的下一个节点next开始遍历链表。 3. 如果cur的值大于next的值,则交换cur和next的位置,即将next节点插入到cur节点之前。 4. 接着继续从cur的下一个节点开始遍历链表,直到链表尾部。 5. 复以上步骤,直到链表中所有节点都被遍历过。 最后在main函数中,我们创建了一个未排序链表,然后调用bubbleSortList函数进行排序,并打印排序后的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FriendshipT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值