Technology sharing

[Leetcode] partitio-list separata coniunctum album

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

- LeetCode

  1. class Solution {
  2. public:
  3. ListNode* partition(ListNode* head, int x) {
  4. ListNode *smlDummy = new ListNode(0), *bigDummy = new ListNode(0);
  5. ListNode *sml = smlDummy, *big = bigDummy;
  6. while (head != nullptr) {
  7. if (head->val < x) {
  8. sml->next = head;
  9. sml = sml->next;
  10. } else {
  11. big->next = head;
  12. big = big->next;
  13. }
  14. head = head->next;
  15. }
  16. sml->next = bigDummy->next;
  17. big->next = nullptr;
  18. return smlDummy->next;
  19. }
  20. };