Leetcode’s daily question
link: 143. Reordering the linked list
problem-solving idea: Convert the linked list into a linear table for storage, and then rearrange it through subscripts.
answer:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head != None:
node_list = []
node = head
while node != None:
node_list.append(node)
node = node.next
node = head
for i in range(int(len(node_list) - 1)):
if i % 2 == 0:
node.next = node_list[-(int(i/2) + 1)]
else:
node.next = node_list[int(i/2 + 1)]
node = node.next
node.next = None