LeetCode //C - 86. Partition List

86. Partition List

Given the head of a linked list and a value x , partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Example 1:

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

Example 2:

Input: head = 2,1, x = 2
Output: 1,2

Constraints:

  • The number of nodes in the list is in the range 0, 200.
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

From: LeetCode

Link: 86. Partition List


Solution:

Ideas:

The main idea behind the code is to maintain two separate linked lists:

  1. Smaller List: This list contains all the nodes with values smaller than x.
  2. Greater or Equal List: This list contains all the nodes with values greater than or equal to x.

The code does the following:

Initialization

  1. We initialize two "dummy" nodes (smallerHead and greaterHead) to serve as the starting points for the two new lists. Dummy nodes simplify the code by eliminating special cases for the head nodes of the lists.

Traversal

  1. We then traverse the original list (head). For each node:
  • If its value is smaller than x, we add it to the end of the "Smaller List" and move the smaller pointer ahead.
  • Otherwise, we add it to the end of the "Greater or Equal List" and move the greater pointer ahead.

Concatenation

  1. Once we've gone through all the nodes, we concatenate the "Smaller List" and the "Greater or Equal List" by setting the next of the last node in the "Smaller List" to the first node in the "Greater or Equal List".

Cleanup

  1. Finally, we return the node following the smallerHead dummy node as the new head of the combined list. We also free the memory allocated for the dummy nodes.
Code:
c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* partition(struct ListNode* head, int x) {
    // Initialize two new dummy nodes to serve as the starting points for the two new lists.
    struct ListNode *smallerHead = (struct ListNode *)malloc(sizeof(struct ListNode));
    struct ListNode *greaterHead = (struct ListNode *)malloc(sizeof(struct ListNode));
    smallerHead->val = 0;
    greaterHead->val = 0;
    smallerHead->next = NULL;
    greaterHead->next = NULL;
    
    struct ListNode *smaller = smallerHead;
    struct ListNode *greater = greaterHead;
    
    // Traverse the original list
    while (head != NULL) {
        if (head->val < x) {
            smaller->next = head;
            smaller = smaller->next;
        } else {
            greater->next = head;
            greater = greater->next;
        }
        head = head->next;
    }
    
    // Connect the two lists
    smaller->next = greaterHead->next;
    greater->next = NULL;
    
    // The new head of the list is the node following the smaller dummy node.
    struct ListNode *newHead = smallerHead->next;
    
    // Free the dummy nodes
    free(smallerHead);
    free(greaterHead);
    
    return newHead;
}
相关推荐
小欣加油18 小时前
leetcode56 合并区间
c++·算法·leetcode·职场和发展
lqqjuly19 小时前
前沿算法深度解析(二)
人工智能·算法·机器学习
徐小夕20 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
akunkuntaimei20 小时前
2026年高考数学各省真题及答案(完整版)
算法·高考
Hello:CodeWorld21 小时前
C 风格变参 vs C++ 变参模板:核心区别与选型指南
c语言·c++·算法
8Qi81 天前
LeetCode 516:最长回文子序列
算法·leetcode·职场和发展·动态规划
十月的皮皮1 天前
C语言学习笔记20260606- 求月份天数三种写法
c语言·笔记·学习
youngerwang1 天前
【从搬运工到协处理器:网卡芯片架构、算法、验证与边缘演进深度剖析】
网络·算法·架构·芯片
KaMeidebaby1 天前
卡梅德生物技术快报|纯化重组蛋白实操详解
人工智能·python·tcp/ip·算法·机器学习
caimouse1 天前
Reactos 第 5 章 进程与线程 — 5.8 Windows 的 APC 机制
c语言·windows