王廷瑋|數位醫療|智慧醫療: 86. Partition List WFU

2024年7月5日 星期五

86. Partition List

86. Partition List


給定一個鏈表的頭節點和一個值 x,將其分割為兩個部分,使得所有小於 x 的節點都在大於或等於 x 的節點之前。

你應該保留這兩個分區中節點的原始相對順序。

範例 1:

輸入:head = [1,4,3,2,5,2], x = 3 輸出:[1,2,2,4,3,5]


Python


from typing import Optional

# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
# Create two dummy nodes to start the less and greater/equal lists
less_head = ListNode(0)
greater_head = ListNode(0)
# Pointers to the current node in the less and greater/equal lists
less = less_head
greater = greater_head
# Traverse the original list
current = head
while current:
if current.val < x:
less.next = current
less = less.next
else:
greater.next = current
greater = greater.next
current = current.next
# Connect the less list with the greater/equal list
less.next = greater_head.next
# End the greater/equal list
greater.next = None
# Return the head of the combined list
return less_head.next

16.35MB, 32ms


C++


/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
// Create two dummy nodes to start the less and greater/equal lists
ListNode* less_head = new ListNode(0);
ListNode* greater_head = new ListNode(0);

// Pointers to the current node in the less and greater/equal lists
ListNode* less = less_head;
ListNode* greater = greater_head;

// Traverse the original list
ListNode* current = head;
while (current != nullptr) {
if (current->val < x) {
less->next = current;
less = less->next;
} else {
greater->next = current;
greater = greater->next;
}
current = current->next;
}

// Connect the less list with the greater/equal list
less->next = greater_head->next;
// End the greater/equal list
greater->next = nullptr;

// Get the head of the combined list
ListNode* new_head = less_head->next;

// Free the dummy nodes
delete less_head;
delete greater_head;

return new_head;
}
};

13.38MB, 0ms


Javascript

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val);
* this.next = (next===undefined ? null : next);
* }
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function(head, x) {
// Create two dummy nodes to start the less and greater/equal lists
let lessHead = new ListNode(0);
let greaterHead = new ListNode(0);

// Pointers to the current node in the less and greater/equal lists
let less = lessHead;
let greater = greaterHead;

// Traverse the original list
let current = head;
while (current !== null) {
if (current.val < x) {
less.next = current;
less = less.next;
} else {
greater.next = current;
greater = greater.next;
}
current = current.next;
}

// Connect the less list with the greater/equal list
less.next = greaterHead.next;
// End the greater/equal list
greater.next = null;

// Return the head of the combined list
return lessHead.next;
};

51.17MB, 57ms