码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C语言 | Leetcode C语言题解之第148题排序链表


    题目:

    题解:

    1. struct ListNode* merge(struct ListNode* head1, struct ListNode* head2) {
    2. struct ListNode* dummyHead = malloc(sizeof(struct ListNode));
    3. dummyHead->val = 0;
    4. struct ListNode *temp = dummyHead, *temp1 = head1, *temp2 = head2;
    5. while (temp1 != NULL && temp2 != NULL) {
    6. if (temp1->val <= temp2->val) {
    7. temp->next = temp1;
    8. temp1 = temp1->next;
    9. } else {
    10. temp->next = temp2;
    11. temp2 = temp2->next;
    12. }
    13. temp = temp->next;
    14. }
    15. if (temp1 != NULL) {
    16. temp->next = temp1;
    17. } else if (temp2 != NULL) {
    18. temp->next = temp2;
    19. }
    20. return dummyHead->next;
    21. }
    22. struct ListNode* sortList(struct ListNode* head) {
    23. if (head == NULL) {
    24. return head;
    25. }
    26. int length = 0;
    27. struct ListNode* node = head;
    28. while (node != NULL) {
    29. length++;
    30. node = node->next;
    31. }
    32. struct ListNode* dummyHead = malloc(sizeof(struct ListNode));
    33. dummyHead->next = head;
    34. for (int subLength = 1; subLength < length; subLength <<= 1) {
    35. struct ListNode *prev = dummyHead, *curr = dummyHead->next;
    36. while (curr != NULL) {
    37. struct ListNode* head1 = curr;
    38. for (int i = 1; i < subLength && curr->next != NULL; i++) {
    39. curr = curr->next;
    40. }
    41. struct ListNode* head2 = curr->next;
    42. curr->next = NULL;
    43. curr = head2;
    44. for (int i = 1; i < subLength && curr != NULL && curr->next != NULL;
    45. i++) {
    46. curr = curr->next;
    47. }
    48. struct ListNode* next = NULL;
    49. if (curr != NULL) {
    50. next = curr->next;
    51. curr->next = NULL;
    52. }
    53. struct ListNode* merged = merge(head1, head2);
    54. prev->next = merged;
    55. while (prev->next != NULL) {
    56. prev = prev->next;
    57. }
    58. curr = next;
    59. }
    60. }
    61. return dummyHead->next;
    62. }
  • 相关阅读:
    【Proteus仿真】【STM32单片机】大棚远程监测控制
    Ubuntu中apt-get update显示域名解析失败
    MBD工具链的云部署
    【开源】基于SpringBoot的农村物流配送系统的设计和实现
    springboot基于JAVA的邮件过滤系统设计与实现
    Python加解压文件gzip库操作一文详解
    Transformers x SwanLab:可视化NLP模型训练
    Linux系统下的redis集群模式
    unity text 文本符号显示问题与打字机效果的结合
    爬虫框架 Scrapy 详解
  • 原文地址:https://blog.csdn.net/m0_59237910/article/details/139640426
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号