C语言 | Leetcode C语言题解之第86题分隔链表

题目:

题解:

struct ListNode* partition(struct ListNode* head, int x) {
    struct ListNode* small = malloc(sizeof(struct ListNode));
    struct ListNode* smallHead = small;
    struct ListNode* large = malloc(sizeof(struct ListNode));
    struct ListNode* largeHead = large;
    while (head != NULL) {
        if (head->val < x) {
            small->next = head;
            small = small->next;
        } else {
            large->next = head;
            large = large->next;
        }
        head = head->next;
    }
    large->next = NULL;
    small->next = largeHead->next;
    return smallHead->next;
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-16 12:32:14       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-16 12:32:14       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-16 12:32:14       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-16 12:32:14       18 阅读

热门阅读

  1. Sping @Autowired @Value @Resourece依赖注入原理

    2024-05-16 12:32:14       13 阅读
  2. spark分布式预测和保存过程中遇到的问题记录

    2024-05-16 12:32:14       12 阅读
  3. OpenCV 实时目标检测

    2024-05-16 12:32:14       12 阅读
  4. AI技术在内容生产中的革新:效率与质量的双赢

    2024-05-16 12:32:14       12 阅读
  5. 学习MySQL(四):记录的增删改查

    2024-05-16 12:32:14       10 阅读