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;
}
相关推荐
星释7 分钟前
Rust 练习册 :Luhn Trait与Trait实现
网络·算法·rust
ゞ 正在缓冲99%…17 分钟前
leetcode1770.执行乘法运算的最大分数
java·数据结构·算法·动态规划
abcefg_h26 分钟前
链表算法---基本算法操作(go语言版)
算法·链表·golang
小O的算法实验室26 分钟前
2022年IEEE TITS SCI2区TOP,基于切线交点和目标引导策略的无人机自主路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
say_fall1 小时前
C语言容易忽略的小知识点(1)
c语言·开发语言
Mr_Oak1 小时前
【multi-model】moco系列&SimCLR&BEiT
人工智能·深度学习·神经网络·算法·计算机视觉·transformer·对比学习
尼古拉斯·纯情暖男·天真·阿玮1 小时前
动态规划——子序列问题
java·算法·动态规划
立志成为大牛的小牛2 小时前
数据结构——四十、折半查找(王道408)
数据结构·学习·程序人生·考研·算法
孙同学_2 小时前
面试题 16.25. LRU 缓存
leetcode·缓存
王哈哈^_^2 小时前
【完整源码+数据集】蓝莓数据集,yolo11蓝莓成熟度检测数据集 3023 张,蓝莓成熟度数据集,目标检测蓝莓识别算法系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·ai·视觉检测