LeetCode 86 Partition List

标签: 链表类题目 LeetCode 发布于:2022-03-27 08:54:49 编辑于:2022-03-27 09:09:58 浏览量:851

概述

https://leetcode.com/problems/partition-list/

解法

读错题了就离谱,好好审题呀。

class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        auto head1 = new ListNode;
        auto head2 = new ListNode;
        auto a = head1;
        auto b = head2;
        auto d = head;
        while (d) {
            if (d->val < x) {
                a->next = d;
                a = a->next;
            } else {
                b->next = d;
                b = b->next;                
            }
            d = d->next;
        }
        a->next = head2->next;
        b->next = nullptr;
        return head1->next;
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/