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;
}
相关推荐
CoovallyAIHub13 分钟前
全球OCR新标杆!百度0.9B小模型斩获四项SOTA,读懂复杂文档像人一样自然
深度学习·算法·计算机视觉
weixin_3776348416 分钟前
【强化学习】RLMT强制 CoT提升训练效果
人工智能·算法·机器学习
雨落在了我的手上37 分钟前
C语言趣味小游戏----扫雷游戏
c语言·游戏
拾光Ծ41 分钟前
【C++高阶数据结构】红黑树
数据结构·算法
Qiuner1 小时前
《掰开揉碎讲编程-长篇》重生之哈希表易如放掌
数据结构·算法·leetcode·力扣·哈希算法·哈希·一文读懂
cici158741 小时前
基于MATLAB的ADS-B接收机卫星与接收天线初始化实现
算法·matlab
71-31 小时前
C语言——关机小程序(有system()和strcmp()函数的知识点)
c语言·笔记·学习
木井巳1 小时前
[Java数据结构与算法]详解排序算法
java·数据结构·算法·排序算法
_Hansen_2 小时前
【C】利用GCC扩展属性进行格式化字符串的静态检查
c语言·产品安全
美狐美颜SDK开放平台2 小时前
直播美颜SDK功能开发实录:自然妆感算法、人脸跟踪与AI美颜技术
人工智能·深度学习·算法·美颜sdk·直播美颜sdk·美颜api