Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Desolve
Created January 8, 2020 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/ba6d096a47b698a49714560a9c591f88 to your computer and use it in GitHub Desktop.
Save Desolve/ba6d096a47b698a49714560a9c591f88 to your computer and use it in GitHub Desktop.
0086 Partition List
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
d1, d2 = ListNode(0), ListNode(0)
ite = [d1, d2]
while head:
nxt = head.next
i = 0 if head.val < x else 1
ite[i].next = head
ite[i] = ite[i].next
ite[i].next = None;
head = nxt
if not d1.next: return d2.next
if not d2.next: return d1.next
ite[0].next = d2.next
return d1.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment