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;
}
相关推荐
可编程芯片开发20 分钟前
基于PI控制算法的pwm直流电机控制系统Simulink建模与仿真
算法
怕浪猫40 分钟前
2840亿参数只卖白菜价:DeepSeek V4 Flash 正式版上线,Agent 能力暴涨6倍
人工智能·算法
让学习成为一种生活方式1 小时前
苄基异喹啉生物碱糖基转移酶UGT74AN1晶体--Journal of Agricultural and Food Chemistry
人工智能·算法
alphaTao1 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
2501_926978331 小时前
提示工程的实战报告(二):模型的失败模式与边界行为
人工智能·深度学习·算法
小小晓.1 小时前
C++记:函数
开发语言·c++·算法
casual~2 小时前
模逆元计算方法详解:扩展欧几里得算法与费马小定理
学习·算法·逆元
脚踏实地皮皮晨2 小时前
002002002_DepandencyObject类2
开发语言·windows·算法·c#·visual studio
NoteStream3 小时前
【c语言基础】C语言常见概念
c语言·开发语言·编辑器
回眸不遇3 小时前
将 Docker虚拟磁盘文件ext.vhdx迁移出C盘 ,更换到D盘
c语言·docker·容器