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");
}
相关推荐
CodeByV2 分钟前
【算法题】链表
数据结构·算法
小杨同学492 分钟前
【嵌入式 C 语言实战】单链表的完整实现与核心操作详解
后端·算法·架构
Tandy12356_5 分钟前
手写TCP/IP协议栈——TCP数据接收
c语言·网络·网络协议·tcp/ip·计算机网络
CQ_YM12 分钟前
SQLite3 数据库与网页html
c语言·数据库·sqlite·html
源代码•宸18 分钟前
Golang原理剖析(map)
经验分享·后端·算法·golang·哈希算法·散列表·map
wen__xvn21 分钟前
代码随想录算法训练营DAY15第六章 二叉树part03
数据结构·算法·leetcode
Sagittarius_A*23 分钟前
图像滤波:手撕五大经典滤波(均值 / 高斯 / 中值 / 双边 / 导向)【计算机视觉】
图像处理·python·opencv·算法·计算机视觉·均值算法
seeksky23 分钟前
Transformer 注意力机制与序列建模基础
算法
冰暮流星23 分钟前
c语言如何实现字符串复制替换
c语言·c++·算法
Swift社区24 分钟前
LeetCode 374 猜数字大小 - Swift 题解
算法·leetcode·swift