LeetCode之“链表”:Reorder List

  题目链接

  题目要求:

  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}.

  刚看到题目,第一个冒出来的想法如下:

  

  对应程序如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void reorderList(ListNode* head) {
12         if (!head || !head->next || !head->next->next)
13             return;
14
15         ListNode *start = head, *end = nullptr;
16         bool flag = true;
17         while (start->next && start->next->next)
18         {
19             if (flag)
20                 head = start;
21             flag = false;
22
23             end = start;
24             auto preEnd = end;
25             while (end->next)
26             {
27                 preEnd = end;
28                 end = end->next;
29             }
30
31             preEnd->next = nullptr;
32             auto next = start->next;
33             start->next = end;
34             end->next = next;
35
36             start = next;
37         }
38     }
39 };

  但超时了!!!

  后来参考了网上别人的通用解法:

  1)用快慢指针找到中间节点,将链表分成两部分。

  2)对后面一半的链表逆序,这个也是常见的问题了(链表反转)。

  3)合并两个链表。

  具体程序如下(72ms):

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void reorderList(ListNode* head) {
12         if (!head || !head->next || !head->next->next)
13             return;
14
15         ListNode *slow = head, *fast = head;
16         while(fast->next && fast->next->next)
17         {
18             slow = slow->next;
19             fast = fast->next->next;
20         }
21
22         ListNode *mid = slow->next;
23         ListNode *prev = nullptr;
24         while(mid)
25         {
26             ListNode *next = mid->next;
27             mid->next = prev;
28             prev = mid;
29             mid = next;
30         }
31         slow->next = nullptr;
32
33         ListNode *start = head;
34         bool flag = true;
35         while (start && prev)
36         {
37             ListNode *next = start->next;
38             start->next = prev;
39             prev = prev->next;
40             start->next->next = next;
41
42             if (flag)
43                 head = start;
44             flag = false;
45
46             start = next;
47         }
48
49     }
50 };
时间: 2024-08-10 14:45:10

LeetCode之“链表”:Reorder List的相关文章

LeetCode解题报告:Reorder List

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 this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 1.利用快慢两个指针将链表一分为二: 2.针对第二个子链表求倒序

[LeetCode系列]链表环探测问题II

给定一个链表头, 探测其是否有环, 如果没有返回NULL, 如果有返回环开始的位置. 环开始的位置定义为被两个指针指向的位置. 算法描述: 1. 快慢指针遍历, 如果到头说明无环返回NULL, 如果相遇说明有环, 进入2. 2. 慢指针回到起点, 快慢指针每次移动一格直到相遇, 返回快指针/慢指针. 代码: 1 class Solution { 2 public: 3 ListNode *detectCycle(ListNode *head) { 4 if (!head || !head->ne

【算法题 14 LeetCode 147 链表的插入排序】

算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): """ :type head: ListNode

LeetCode 单链表专题 (一)

目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Partition List \([82]\) Remove Duplicates from Sorted List II \([61]\) Rotate List \([19]\) Remove Nth Node From End of List LeetCode 单链表专题 <c++> \([2]\)

LeetCode 206 链表 Reverse Linked List

LeetCode 206 链表 Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement

LeetCode 142 链表 Linked List Cycle II

LeetCode 142 链表 Linked List Cycle II LeetCode Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexe

LeetCode OJ 143. 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 this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. Subscribe to see which companies asked this quest

leetcode || 143、Reorder List

problem: 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 this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. Hide Tags Linked List 题意:对单链表重新排序,依次将尾

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =