LeetCode 86 Partition List

Tag: 链表类题目 LeetCode Posted on 2022-03-27 08:54:49 Edited on 2022-03-27 09:09:58 Views: 322

概述

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/