【10月打卡~Leetcode每日一题】143. 重排链表(难度:中等)

it2023-05-16  73

143. 重排链表

# Definition for singly-linked list. # 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 not head: return node = head Node_list = list() while(node): Node_list.append(node) node = node.next flag = False while(Node_list): if(flag): right = Node_list.pop() head.next = right head = head.next flag = False else: left = Node_list.pop(0) head.next = left head = head.next flag = True head.next = None

时间复杂度O(n) 空间复杂度O(n)

做题过程中深感自己数据结构基础的薄弱(并没有学过这门课,只是在做题的时候尝试着理解),这段时间看看浙大的数据结构课吧,补短。

最新回复(0)