LeetCode //C - 143. Reorder List

143. Reorder List

You are given the head of a singly linked-list. The list can be represented as:

L0 → L1 → ... → Ln - 1 → Ln

Reorder the list to be on the following form:

L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → ...

You may not modify the values in the list's nodes. Only nodes themselves may be changed.

Example 1:

Input: head = 1,2,3,4
Output: 1,4,2,3

Example 2:

Input: head = 1,2,3,4,5
Output: 1,5,2,4,3

Constraints:
  • The number of nodes in the list is in the range 1 , 5 ∗ 1 0 4 1, 5 \* 10\^4 1,5∗104.
  • 1 <= Node.val <= 1000

From: LeetCode

Link: 143. Reorder List


Solution:

Ideas:
  1. Find the middle of the linked list: We can use the fast and slow pointer technique to find the middle node.
  2. Reverse the second half of the linked list: Once we find the middle, we need to reverse the second half of the list.
  3. Merge the two halves: Finally, we merge the two halves by alternating nodes from the first half and the reversed second half.
Code:
cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void reorderList(struct ListNode* head) {
    if (!head || !head->next) return;

    // Step 1: Find the middle of the linked list
    struct ListNode *slow = head, *fast = head;
    while (fast->next && fast->next->next) {
        slow = slow->next;
        fast = fast->next->next;
    }

    // Step 2: Reverse the second half of the list
    struct ListNode *prev = NULL, *curr = slow->next, *next = NULL;
    while (curr) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    slow->next = NULL; // Cut the list into two halves

    // Step 3: Merge the two halves
    struct ListNode *first = head, *second = prev;
    while (second) {
        struct ListNode *tmp1 = first->next, *tmp2 = second->next;
        first->next = second;
        second->next = tmp1;
        first = tmp1;
        second = tmp2;
    }
}

// Helper function to create a new ListNode
struct ListNode* newNode(int val) {
    struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
    node->val = val;
    node->next = NULL;
    return node;
}

// Helper function to print the linked list
void printList(struct ListNode* head) {
    while (head) {
        printf("%d -> ", head->val);
        head = head->next;
    }
    printf("NULL\n");
}
相关推荐
郝学胜-神的一滴31 分钟前
完全二叉树与堆底层原理深度剖析 | 手写C++大顶堆实现
java·开发语言·数据结构·c++·python·算法
青山木33 分钟前
Hot 100 --- 缺失的第一个正数
算法·leetcode·哈希算法
农民小飞侠34 分钟前
[leetcode] 165. Compare Version Numbers
java·算法·leetcode
装不满的克莱因瓶1 小时前
掌握语义分割经典模型 FCN——从像素分类到端到端分割的奠基之作
人工智能·python·深度学习·算法·机器学习·分类·数据挖掘
学计算机的计算基1 小时前
链表算法上篇:LeetCode 206/234/141/142/160/21 题解与易错点
java·笔记·算法·链表
大白话_NOI1 小时前
【洛谷 P2678】 [NOIP2015 提高组] 跳石头 超详细题解
c++·算法
xwz小王子1 小时前
ICRA 2026深度观察:全栈闭环成标配,中国具身智能势力显著崛起
大数据·人工智能·算法
孬甭_1 小时前
深入解析归并排序:稳定高效的分治典范
算法·排序算法
DXM05211 小时前
第14期|高阶分割模型:Transformer/SegFormer遥感应用
人工智能·python·神经网络·算法·计算机视觉·cnn·ageo