Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 7. 22. 23:45

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

 

Partition List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

어제에 이어 또 리스트 문제

class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        before = before_head = ListNode(0)
        after = after_head = ListNode(0)

        while head:            
            if head.val < x:
                before.next = head
                before = before.next
            else:                
                after.next = head
                after = after.next            
            head = head.next

        after.next = None
        before.next = after_head.next

        return before_head.next
반응형