Solution: Partition List

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode’s forums.

Leetcode Problem #86 (Medium): Partition List


This content originally appeared on DEV Community and was authored by seanpgallivan

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.

Leetcode Problem #86 (Medium): Partition List

Description:


(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Examples:

Example 1:
Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]
Visual: Example 1 Visual
Example 2:
Input: head = [2,1], x = 2
Output: [1,2]

Constraints:

  • The number of nodes in the list is in the range [0, 200].
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

Idea:


(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)

The easiest thing to do here is to create separate linked lists for the front and back portions of list we want to return. In order to accomplish that, we should first create some dummy heads (fdum, bdum), then create pointers for the current nodes each of the front, back, and main lists (front, back, curr).

Then we can simply iterate through the main list and stitch together each node to either front or back, depending on the node's value.

Once we reach the end, we just need to stitch together the two sub-lists, making sure to cap off the end of back, and then return our new list, minus the dummy head.

Implementation:

There are only minor differences between the code of all four languages.

Javascript Code:


(Jump to: Problem Description || Solution Idea)

var partition = function(head, x) {
    let fdum = new ListNode(0), bdum = new ListNode(0),
        front = fdum, back = bdum, curr = head
    while (curr) {
        if (curr.val < x)front.next = curr, front = curr
        else back.next = curr, back = curr
        curr = curr.next
    }
    front.next = bdum.next, back.next = null
    return fdum.next
};

Python Code:


(Jump to: Problem Description || Solution Idea)

class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        fdum, bdum = ListNode(0), ListNode(0)
        front, back, curr = fdum, bdum, head
        while curr:
            if curr.val < x:
                front.next = curr
                front = curr
            else:
                back.next = curr
                back = curr
            curr = curr.next
        front.next, back.next = bdum.next, None
        return fdum.next

Java Code:


(Jump to: Problem Description || Solution Idea)

class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode fdum = new ListNode(0), bdum = new ListNode(0),
                 front = fdum, back = bdum, curr = head;
        while (curr != null) {
            if (curr.val < x) {
                front.next = curr;
                front = curr;
            } else {
                back.next = curr;
                back = curr;
            }
            curr = curr.next;
        }
        front.next = bdum.next;
        back.next = null;
        return fdum.next;
    }
}

C++ Code:


(Jump to: Problem Description || Solution Idea)

class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode *fdum = new ListNode(0), *bdum = new ListNode(0),
                 *front = fdum, *back = bdum, *curr = head;
        while (curr) {
            if (curr->val < x) front->next = curr, front = curr;
            else back->next = curr, back = curr;
            curr = curr->next;
        }
        front->next = bdum->next, back->next = nullptr;
        return fdum->next;
    }
};


This content originally appeared on DEV Community and was authored by seanpgallivan


Print Share Comment Cite Upload Translate Updates
APA

seanpgallivan | Sciencx (2021-04-14T07:58:52+00:00) Solution: Partition List. Retrieved from https://www.scien.cx/2021/04/14/solution-partition-list/

MLA
" » Solution: Partition List." seanpgallivan | Sciencx - Wednesday April 14, 2021, https://www.scien.cx/2021/04/14/solution-partition-list/
HARVARD
seanpgallivan | Sciencx Wednesday April 14, 2021 » Solution: Partition List., viewed ,<https://www.scien.cx/2021/04/14/solution-partition-list/>
VANCOUVER
seanpgallivan | Sciencx - » Solution: Partition List. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/14/solution-partition-list/
CHICAGO
" » Solution: Partition List." seanpgallivan | Sciencx - Accessed . https://www.scien.cx/2021/04/14/solution-partition-list/
IEEE
" » Solution: Partition List." seanpgallivan | Sciencx [Online]. Available: https://www.scien.cx/2021/04/14/solution-partition-list/. [Accessed: ]
rf:citation
» Solution: Partition List | seanpgallivan | Sciencx | https://www.scien.cx/2021/04/14/solution-partition-list/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.