https://oj.leetcode.com/problems/reorder-list/

将一个链表重新排序,比如 1 2 3 4 5,变成 1 5 2 4 3

1.找到中间节点 mid

2.将链表分成两部分 head,head2

3.将head2 逆序

4.将head head2,再合并成一个链表

  1. class Solution {
  2. public:
  3. void reorderList(ListNode *head) {
  4. //0 elements, 1 elements, 2 elements
  5. if(head == NULL || head != NULL && head->next == NULL || head!= NULL && head->next != NULL && head->next->next == NULL)
  6. return;
  7.  
  8. //find middle
  9. ListNode *end = head;
  10. ListNode *mid = head;
  11. while(end)
  12. {
  13. end = end->next;
  14. if(end && end->next)
  15. {
  16. end = end->next;
  17. mid = mid->next;
  18. }
  19. }
  20. ListNode *head2 = mid->next;
  21.  
  22. //split
  23. mid->next = NULL;
  24.  
  25. //reverse head2
  26. ListNode *itr = head2;
  27. if(head2->next)
  28. {
  29. head2 = head2->next;
  30. itr->next = NULL;
  31. ListNode *tmp = NULL;
  32. while(head2)
  33. {
  34. tmp = head2;
  35. head2 = head2->next;
  36. tmp->next = itr;
  37. itr = tmp;
  38. }
  39. head2 = itr;
  40. }
  41.  
  42. //union
  43. ListNode *dummy = new ListNode();
  44. ListNode *node1 = head;
  45. while(node1)
  46. {
  47. dummy->next = node1;
  48. node1 = node1->next;
  49. dummy = dummy->next;
  50. if(head2)
  51. {
  52. dummy->next = head2;
  53. head2 = head2->next;
  54. dummy = dummy->next;
  55. }
  56. else
  57. break;
  58. }
  59. }
  60. };

LeetCode OJ-- Reorder List **的更多相关文章

  1. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. Base64及其Python实现

    1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...

  2. 最小生成树:HDU1863-畅通工程

    畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  3. 多进程的基本使用--multiprocessing 【转】

    multiprocessing 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择.由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序? 由 ...

  4. 利用本地SQL Server维护计划来维护SQL Database

    On-Premise的SQL Server提供了维护计划来定期.定时的维护SQL Server.一般的做法是:定义SQL Server Agent Jobs,而后维护计划帮助我们定期.定时执行SQL ...

  5. mysql-copy to tmp table

    今天数据后台数据反映有些迟缓后查看链接 processlist 发下好多 锁 和磁盘写入,   参考文章 : http://bbs.chinaunix.net/forum.php?mod=viewth ...

  6. VMware之无法切换桥接网络

    一.关闭正在运行的虚拟机 二.打开虚拟网络编辑器 三.还原默认设置 四.启动虚拟机即可正常使用桥接网络

  7. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  8. easyui的layout

    1.浏览器自适应(即浏览器改变大小,里面的表格大小也会随之改变)要设置两个参数 (1)一般都要在body上设置class=“easyui-layout”: <body class="e ...

  9. 二叉树遍历(Java实现)

    二叉树遍历(Java实现)   主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...

  10. Python框架之Django学习笔记(十五)

    表单 从Google的简朴的单个搜索框,到常见的Blog评论提交表单,再到复杂的自定义数据输入接口,HTML表单一直是交互性网站的支柱.本次内容将介绍如何用Django对用户通过表单提交的数据进行访问 ...