...
| Code Block | ||
|---|---|---|
| ||
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
# collect value serially
v=[]
p=head
while p!=None:
v.append(p.val)
p=p.next
# reorder based on the given logic
i=0
lcnt=len(v)
j=lcnt-1
p=head
while p!=None:
p.val = v[i]
i += 1
p=p.next
if p!=None:
p.val = v[j]
j-=1
p = p.next
|
Compact - Remove Falsy values
| Code Block |
|---|
def compact(lst):
return list(filter(None, lst))
compact([0, 1, False, 2, '', 3, 'a', 's', 34]) # [ 1, 2, 3, 'a', 's', 34 ] |