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;
}
相关推荐
F-2H1 小时前
C语言:指针4(常量指针和指针常量及动态内存分配)
java·linux·c语言·开发语言·前端·c++
axxy20002 小时前
leetcode之hot100---24两两交换链表中的节点(C++)
c++·leetcode·链表
chenziang12 小时前
leetcode hot100 环形链表2
算法·leetcode·链表
Captain823Jack3 小时前
nlp新词发现——浅析 TF·IDF
人工智能·python·深度学习·神经网络·算法·自然语言处理
Captain823Jack4 小时前
w04_nlp大模型训练·中文分词
人工智能·python·深度学习·神经网络·算法·自然语言处理·中文分词
是小胡嘛4 小时前
数据结构之旅:红黑树如何驱动 Set 和 Map
数据结构·算法
m0_748255024 小时前
前端常用算法集合
前端·算法
呆呆的猫5 小时前
【LeetCode】227、基本计算器 II
算法·leetcode·职场和发展
Tisfy5 小时前
LeetCode 1705.吃苹果的最大数目:贪心(优先队列) - 清晰题解
算法·leetcode·优先队列·贪心·
余额不足121385 小时前
C语言基础十六:枚举、c语言中文件的读写操作
linux·c语言·算法