前往此题
辅助栈,模拟法
- 将链表节点放入栈stack中
- 再创建新的链表,按照1->stack.pop()->2->stack.pop()->3 这样的形式进行拼接即可
# 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.
"""
stack = []
cur = head
while cur:
stack.append(cur)
cur = cur.next
cur = head
res = ListNode(0)
while cur.next != res.next:
res = stack.pop()
res.next = cur.next
cur.next = res
cur = cur.next.next
res.next = None