链表分割(牛客)

目录

题目:

举例:

解题思路:

总代码:

图解: 


题目:

现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

举例:

假如列表为且x为4

那么排序后为:

解题思路:

我们设定两个链表,第一个链表存放小于x的数,第二个链表存放大于等于x的数,并为其分别创立一个哨兵位结点

struct ListNode* lesshead,*bighead;
lesshead=(struct ListNode*)malloc(sizeof(struct ListNode));
bighead=(struct ListNode*)malloc(sizeof(struct ListNode));

 我们遍历链表后,结果为

那么我们只需要将,lesshead的最后的3,指向bighead->next,就是3指向4,然后返回lesshead->next即可; 

 所以我们就需要创建遍历列表的cur,以及链接尾部的lesstail和bigtail;

struct ListNode* lesshead,*bighead,*lesstail,*bigtail;
        lesshead=(struct ListNode*)malloc(sizeof(struct ListNode));
        bighead=(struct ListNode*)malloc(sizeof(struct ListNode));
        lesstail=lesshead;
        bigtail=bighead;
        struct ListNode* cur=pHead;

 然后就是实现存放的步骤,这里先给出总代码,下面我将画图详解

总代码:

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        if(pHead==NULL)
        {
            return NULL;
        }
        struct ListNode* lesshead,*bighead,*lesstail,*bigtail;
        lesshead=(struct ListNode*)malloc(sizeof(struct ListNode));
        bighead=(struct ListNode*)malloc(sizeof(struct ListNode));
        lesstail=lesshead;
        bigtail=bighead;
        struct ListNode* cur=pHead;
        while(cur)
        {
            if(cur->val<x)
            {
                lesstail->next=cur;
                lesstail=lesstail->next;
            }
            else
            {
                bigtail->next=cur;
                bigtail=bigtail->next;
            }
            cur=cur->next;
        }
        lesstail->next=bighead->next;
        bigtail->next=NULL;
        pHead=lesshead->next;
        free(lesshead);
        free(bighead);
        return pHead;
    }
};

图解: 

第一步:1小于4,我们将lesshead的哨兵位结点,就是此时lesstail处的next指向cur,然后lesstail再向前进一步,然后cur向后

 第二步:同理

 第三步:同理

 第四步同理

 第五步:此时cur处的val不小于4,所以要将bigtail的next链接到cur,然后cur向后,bigtail也向后

 第六步

第七部:

 第八步

 第九步:此刻cur为空,循环结束,我们只需要将lesstail->next指向bighead->next即可(注意是bighead->next,因为lesshead和bighead我们都创立了哨兵位,如果lesstail直接指向bighead,那么就是指向了bighead的哨兵位),然后free掉lesshead和bighead的哨兵位,返回pHead,(其实不free也可以,保持良好习惯)

 上面介绍的是一般情况,还有一种特殊情况

 如果我们数组的最后一位在lesshead里面倒数第二位在bighead里面。

因为我们的lesstai是指向bigtail的next处,此时就形成了死循环,所以我们需要将bigtail的next置空。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
思路: 1. 创建3个循环链表,分别表示字母、数字和其他字符的线性表。 2. 遍历原链表,将每个节点根据其类型插入到相应的循环链表中。 3. 将每个循环链表的尾节点指向头节点,形成循环链表。 Python代码实现如下: ```python class Node: def __init__(self, val): self.val = val self.next = None def split_list(head): letter_head = letter_tail = Node(None) digit_head = digit_tail = Node(None) other_head = other_tail = Node(None) cur = head while cur: if cur.val.isalpha(): letter_tail.next = cur letter_tail = cur elif cur.val.isdigit(): digit_tail.next = cur digit_tail = cur else: other_tail.next = cur other_tail = cur cur = cur.next # 将每个循环链表的尾节点指向头节点,形成循环链表 letter_tail.next = letter_head.next digit_tail.next = digit_head.next other_tail.next = other_head.next return letter_head.next, digit_head.next, other_head.next ``` 测试代码如下: ```python # 创建测试链表 head = Node('a') node1 = Node('1') node2 = Node('b') node3 = Node('*') node4 = Node('c') node5 = Node('2') node6 = Node('d') node7 = Node('#') node8 = Node('3') head.next = node1 node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 node5.next = node6 node6.next = node7 node7.next = node8 node8.next = None # 测试分割函数 letter_head, digit_head, other_head = split_list(head) # 打印分割结果 cur = letter_head print("字母链表:", end="") while cur != letter_head.prev: print(cur.val, end="") cur = cur.next print() cur = digit_head print("数字链表:", end="") while cur != digit_head.prev: print(cur.val, end="") cur = cur.next print() cur = other_head print("其他字符链表:", end="") while cur != other_head.prev: print(cur.val, end="") cur = cur.next print() ``` 输出结果如下: ``` 字母链表:abcde 数字链表:123 其他字符链表:*#

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值