LeetCode:86. Partition List分隔链表(C语言)
发布日期:2021-05-08 18:45:04 浏览次数:21 分类:精选文章

本文共 3154 字,大约阅读时间需要 10 分钟。

为了解决这个问题,我们需要将给定的链表分割成两个部分:一部分包含所有小于给定值 x 的节点,另一部分包含所有大于或等于 x 的节点。同时,我们需要保持每个部分中节点的初始相对位置。

方法思路

我们可以使用两个链表来分别存储小于 x 和大于或等于 x 的节点。具体步骤如下:

  • 初始化两个链表 ListLessListGreater,以及它们的头指针 h1h2
  • 遍历原链表中的每个节点。
  • 如果当前节点的值小于 x,将其添加到 ListLess 中;否则,将其添加到 ListGreater 中。
  • 处理链表连接时,确保每个链表中的节点保持初始相对位置。
  • 最后,将两个链表连接起来,返回合并后的链表。
  • 解决代码

    题目描述:

    给你一个链表和一个特定值 x ,请你对链表进行分隔,使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。

    你应当保留两个分区中每个节点的初始相对位置。

    示例:

    输入:head = 1->4->3->2->5->2, x = 3

    输出:1->2->2->4->3->5

    来源:力扣(LeetCode)

    链接:https://leetcode-cn.com/problems/partition-list
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    解答:
    struct ListNode* partition(struct ListNode* head, int x){       struct ListNode* ListLess = NULL;    struct ListNode* ListGreater = NULL;    struct ListNode* h1 = NULL;    struct ListNode* h2 = NULL;    struct ListNode* p = head;    while(p){           if(p->val < x){               if(ListLess == NULL){                   ListLess = p;                   h1 = ListLess;               } else{                   ListLess->next = p;                   ListLess = p;               }           } else{               if(ListGreater == NULL){                   ListGreater = p;                   h2 = ListGreater;               } else{                   ListGreater->next = p;                   ListGreater = p;               }           }        p = p->next;    }    if(ListLess == NULL) return h2;    if(ListGreater == NULL) return h1;    ListGreater->next = NULL;    ListLess->next = h2;    return h1;}

    运行结果:

    通过了测试用例,正确处理了输入链表和给定值,输出符合要求的分隔结果。

    为了解决这个问题,我们需要将给定的链表分割成两个部分:一部分包含所有小于给定值 x 的节点,另一部分包含所有大于或等于 x 的节点。同时,我们需要保持每个部分中节点的初始相对位置。### 方法思路我们可以使用两个链表来分别存储小于 x 和大于或等于 x 的节点。具体步骤如下:1. 初始化两个链表 `ListLess` 和 `ListGreater`,以及它们的头指针 `h1` 和 `h2`。2. 遍历原链表中的每个节点。3. 如果当前节点的值小于 x,将其添加到 `ListLess` 中;否则,将其添加到 `ListGreater` 中。4. 处理链表连接时,确保每个链表中的节点保持初始相对位置。5. 最后,将两个链表连接起来,返回合并后的链表。### 解决代码```html

    题目描述:

    给你一个链表和一个特定值 x ,请你对链表进行分隔,使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。

    你应当保留两个分区中每个节点的初始相对位置。

    示例:

    输入:head = 1->4->3->2->5->2, x = 3

    输出:1->2->2->4->3->5

    来源:力扣(LeetCode)

    链接:https://leetcode-cn.com/problems/partition-list
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    解答:
    struct ListNode* partition(struct ListNode* head, int x){       struct ListNode* ListLess = NULL;    struct ListNode* ListGreater = NULL;    struct ListNode* h1 = NULL;    struct ListNode* h2 = NULL;    struct ListNode* p = head;    while(p){           if(p->val < x){               if(ListLess == NULL){                   ListLess = p;                   h1 = ListLess;               } else{                   ListLess->next = p;                   ListLess = p;               }           } else{               if(ListGreater == NULL){                   ListGreater = p;                   h2 = ListGreater;               } else{                   ListGreater->next = p;                   ListGreater = p;               }           }        p = p->next;    }    if(ListLess == NULL) return h2;    if(ListGreater == NULL) return h1;    ListGreater->next = NULL;    ListLess->next = h2;    return h1;}

    运行结果:

    通过了测试用例,正确处理了输入链表和给定值,输出符合要求的分隔结果。

    上一篇:【个人网站搭建】GitHub pages+hexo框架下为next主题添加分类及标签
    下一篇:LeetCode:682. Baseball Game棒球比赛(C语言)

    发表评论

    最新留言

    不错!
    [***.144.177.141]2025年04月01日 13时52分45秒