【图文解析】链表分割


例题描述

编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前。

给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。
(注意:分割以后保持原来的数据顺序不变)

示例:

  • 输入:9->5->2->7x = 4
  • 输出:2->9->5->7

结点结构体定义

typedef struct ListNode {
	int val;
	struct ListNode *next;
}	ListNode;

解题思路

创建两条链表,小于此值得放入小链表,大于此值放入大链表,然后通过尾结点指针将两条链表合并起来。

  1. 判断极端条件,如果链表为空,既给定值也无法分割,直接返回空。
  2. 创建大链表bigger,存放所有大于此值的结点。创建小链表samller,存放所有小于此值的结点。但二者的指向都只是一个假结点是存放了一个无效值的头结点,从它们的next结点才开始存放旧链表中的值。因为这样可以更好的使尾指针进行运作。
  3. 因为需要搬运结点以及链接两链表,所以各自建立一个尾指针,作为搬运工,并初始化为各自头指针的指向。
  4. 创建当前结点cur,为循环判断条件,优先级最高,并初始化为链表首结点。
    在这里插入图片描述
  5. 通过判断当前指针的有效性来决定是否继续执行循环逻辑。如果不为空,进行判定逻辑:
    若当前结点值小于给定值,将它尾插入小链表,并更新小链表尾结点及当前指针的指向。
    若当前结点值大于等于给定值,将它尾插入大链表,并更新大链表尾结点及当前指针的指向。在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
  6. 最后执行链接逻辑,手动将小链表的尾指针smalltailnext指向大链表的biggernext结点。将大链表的尾结点置为NULL,保证链表的有限性。
    在这里插入图片描述
  7. 最终忽略假结点,返回小链表的smallernext结点即可。

实现一

ListNode* partition(ListNode* pHead, int x) {
        if (pHead == NULL) {
	    	return NULL;
	    }
	    //1. 创建两链表
        struct ListNode *smaller = (struct ListNode*)malloc(sizeof(ListNode)*x);
        struct ListNode *bigger = (struct ListNode*)malloc(sizeof(ListNode)*x);
        struct ListNode *cur = pHead;
        struct ListNode *smalltail = smaller;
        struct ListNode *bigtail = bigger;
        while(cur){
            if(cur->val < x){
                smalltail->next = cur;
                smalltail = cur;
            }else{
                bigtail->next = cur;
                bigtail = cur;
            }
            cur = cur->next;
        }
        smalltail->next = bigger->next;
        bigtail->next = NULL;
        return smaller->next;
}

实现二

ListNode* partition(ListNode* pHead, int x) {
		struct ListNode *small = NULL;
        struct ListNode *smalllast = NULL;
        
        struct ListNode *big = NULL;
        struct ListNode *biglast = NULL;
        
        struct ListNode *node = pHead;
        while(node){
            if(node->val < x){
                if(small == NULL){
                    small = smalllast = node;
                }else{
                    smalllast->next = node;
                    smalllast = node;
                }
            }else{
                if(big == NULL){
                    big = biglast = node;
                }else{
                    biglast->next = node;
                    biglast = node;
                }
            }
            node = node->next;
        }
        if(smalllast != NULL){        //一个比他小的都没有
            smalllast->next = big;
        }
        if(biglast != NULL){        //一个比他小的都没有
            biglast->next = NULL;
        }
        if(smalllast != NULL){
            return small;
        }
        else{
            return big;
        }
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux内核中的链表是一种常用的数据结构,它在内核中被广泛使用来组织和管理数据。链表使用双链表的形式,每个节点只包含指针域,没有数据域。在Linux内核中,链表的操作接口定义在`linux/list.h`头文件中。为了方便使用,内核提供了多种初始化链表的方式。宏定义是一种常用的初始化链表的方式,其中的一个宏定义是`INIT_LIST_HEAD(ptr)`。这个宏被用来初始化一个链表节点,将其next和prev指针都指向自身,表示这是一个空链表。通过这样的方式,可以方便地对链表进行插入和删除操作,以及遍历链表中的元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Linux内核链表理解与使用](https://blog.csdn.net/to_be_better_wen/article/details/127720433)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [linux内核链表最细讲解](https://blog.csdn.net/yzw_yy/article/details/130094799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Linux中的内核链表实例详解](https://download.csdn.net/download/weixin_38724363/14893522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

giturtle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值