Reorder List

2014.1.13 22:07

Given a singly linked list LL0→L1→…→Ln-1→Ln,
reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

Solution1:

  My solution for this problem is in three steps:

    1. cut the list in two halves of equal length (maybe differ by 1).

    2. reverse the latter one.

    3. merge them in a crossing manner: 1->2->1->2->1->...

  Time complexity is O(n), space complexity is O(1), where n is the number of nodes in the list.

Accepted code:

 1 // 4CE, 1AC, if you're able to design the right algorithm with just one shot, why failed by 4 foolish CEs?
 2 /**
 3  * Definition for singly-linked list.
 4  * struct ListNode {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     void reorderList(ListNode *head) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         if(head == nullptr){
16             // 1CE here, void function...
17             return;
18         }
19
20         int n, n1, n2;
21         // 1CE here, ListNode, not List...
22         ListNode *h1, *h2, *ptr;
23
24         h1 = head;
25         ptr = head;
26         n = 0;
27         while(ptr != nullptr){
28             ++n;
29             ptr = ptr->next;
30         }
31         n1 = (n + 1) / 2;
32         n2 = n - n1;
33         ptr = head;
34         // 1CE here, declaration for $i missing
35         for(int i = 1; i < n1; ++i){
36             ptr = ptr->next;
37         }
38         h2 = ptr->next;
39         ptr->next = nullptr;
40         h2 = reverseList(h2);
41
42         ListNode *root = new ListNode(0), *tail;
43         ListNode *p1, *p2;
44         tail = root;
45         while(h1 != nullptr || h2 != nullptr){
46             if(h1 != nullptr){
47                 tail->next = h1;
48                 h1 = h1->next;
49                 tail = tail->next;
50                 tail->next = nullptr;
51             }
52             if(h2 != nullptr){
53                 tail->next = h2;
54                 h2 = h2->next;
55                 tail = tail->next;
56                 tail->next = nullptr;
57             }
58         }
59
60         head = root->next;
61         delete root;
62         // 1CE here, void function has no return
63     }
64 private:
65     ListNode *reverseList(ListNode *head) {
66         if(nullptr == head){
67             return head;
68         }
69
70         ListNode *ptr1, *ptr2, *root;
71
72         root = new ListNode(0);
73         ptr1 = head;
74         while(ptr1 != nullptr){
75             ptr2 = root->next;
76             root->next = ptr1;
77             ptr1 = ptr1->next;
78             root->next->next = ptr2;
79         }
80
81         head = root->next;
82         delete root;
83         return head;
84     }
85 };

Solution2:

  I've noticed that I used an extra new operation in the code. This overhead can be avoided.

Accepted code:

 1 // 2WA, 1AC, not so satisfactory
 2 class Solution {
 3 public:
 4     void reorderList(ListNode *head) {
 5         if(head == nullptr){
 6             return;
 7         }
 8
 9         int n, n1, n2;
10         ListNode *h1, *h2, *ptr;
11
12         h1 = head;
13         ptr = head;
14         n = 0;
15         while(ptr != nullptr){
16             ++n;
17             ptr = ptr->next;
18         }
19         n1 = (n + 1) / 2;
20         n2 = n - n1;
21         ptr = head;
22         for(int i = 1; i < n1; ++i){
23             ptr = ptr->next;
24         }
25         h2 = ptr->next;
26         ptr->next = nullptr;
27         h2 = reverseList(h2);
28
29         ListNode *tail;
30         ListNode *p1, *p2;
31         tail = nullptr;
32         head = h1;
33         while(h1 != nullptr || h2 != nullptr){
34             if(h1 != nullptr){
35                 if(tail != nullptr){
36                     tail->next = h1;
37                     tail = tail->next;
38                 }else{
39                     tail = h1;
40                 }
41                 h1 = h1->next;
42                 tail->next = nullptr;
43             }
44             if(h2 != nullptr){
45                 if(tail != nullptr){
46                     tail->next = h2;
47                     tail = tail->next;
48                 }else{
49                     tail = h2;
50                 }
51                 h2 = h2->next;
52                 tail->next = nullptr;
53             }
54         }
55     }
56 private:
57     ListNode *reverseList(ListNode *head) {
58         if(nullptr == head){
59             return head;
60         }
61
62         ListNode *ptr1, *ptr2;
63
64         ptr1 = head;
65         head = nullptr;
66         while(ptr1 != nullptr){
67             ptr2 = ptr1;
68             ptr1 = ptr1->next;
69             if(head != nullptr){
70                 // 2WA here, better manually debug it before submission
71                 ptr2->next = head;
72                 head = ptr2;
73             }else{
74                 head = ptr2;
75                 ptr2->next = nullptr;
76             }
77         }
78
79         return head;
80     }
81 };

转载于:https://www.cnblogs.com/zhuli19901106/p/3518141.html

LeetCode - Reorder List相关推荐

  1. [Leetcode] Reorder List

    Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do th ...

  2. Leetcode: Reorder List

    FROM 思路 1. 将后半段截取下来再倒序, 插入到前半段, 时间复杂度为 o(n) 代码 #include <iostream> using namespace std;struct ...

  3. LeetCode Reorder List

    题意:给出一个链表,重新排列链表,形式为第一个-->倒数第一个->第二个->倒数第二个 思路:将数组分成两半,然后将后半部反转,再拼接 代码发下: class Solution {p ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. [LeetCode] 143. Reorder List_Middle tag: Linked List

    Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You may not mo ...

  6. 【Leetcode】143. Reorder List

    Question: Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You ...

  7. 【重点】LeetCode 143. Reorder List

    LeetCode 143. Reorder List Solution1: 参考网址:http://www.cnblogs.com/grandyang/p/4254860.html 这段代码有值得学习 ...

  8. leetcode之Reorder List

    Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do th ...

  9. LeetCode:937. Reorder Log Files

    051401 题目 You have an array of logs. Each log is a space delimited string of words. For each log, th ...

最新文章

  1. HarmonyOS Text超出部分末尾显示...
  2. c语言二十四点游戏,C语言解24点游戏程序
  3. 统计决策理论1 统计问题与统计决策
  4. Spark HistoryServer日志解析清理异常
  5. php的静态变量static在函数内部
  6. python爬取天气数据山东_Python的学习《山东省各城市天气爬取》
  7. 怎么让模糊的数字变清楚_一键模糊图像变清晰,好家伙!这款神器插件你值得拥有...
  8. hbase major_compact 文件会变小吗_图解式学习:可能是最易懂的Hbase架构原理解析(二)...
  9. SpringMVC@RequestMapping请求方法限定与请求参数限定
  10. 如何对shell脚本进行加密且不影响脚本运行
  11. 敏捷个人A1组第三次练习讨论 你是如何管理你的精力的?
  12. mysql数据改电话号码_mysql 里面用什么数据类型来定义手机号码?
  13. Linux内核配置Kconfig
  14. win10虚拟桌面快捷键
  15. mysql 索引的模糊查询_MYSQL语法(模糊查询,视图,索引)
  16. C# 中国大陆二代身份证号生成及格式验证
  17. 克罗内克积(Kronecker)
  18. 网管员必知:常用电脑密码破解
  19. mac使用Simulator打开IOS模拟器(不使用Xcode打开IOS模拟器)
  20. Parallel Tracking and Verifying: A Framework for Real-Time and High Accuracy(PTAV)论文笔记

热门文章

  1. CentOS6.0搭建DNS
  2. Android应用程序键盘(Keyboard)消息处理机制分析(3)
  3. Apache将整合Google Wave功能
  4. Error: Password file read access must be restricted: /etc/cassandra/jmxremote.password
  5. 交换机定时自动备份配置文件的方法
  6. Elasticsearch上手——熟悉基本操作
  7. 问题-[Delphi]通过Map文件查找内存地址出错代码所在行
  8. 给一个金额字符串插入逗号分隔 保留两位有效数字
  9. 如何确定一个IAR工程所使用的IAR版本
  10. MySQL学习笔记_9_MySQL高级操作(上)