-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143.重排链表.py
42 lines (38 loc) · 1022 Bytes
/
143.重排链表.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# @lc app=leetcode.cn id=143 lang=python3
#
# [143] 重排链表
#
# %%
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head):
prev, curr = None, head
while curr is not None:
curr.next, prev, curr = prev, curr, curr.next
return prev
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
slow, fast = head, head.next
while fast is not None and fast.next is not None:
fast = fast.next.next
slow = slow.next
h, m = head, slow.next
slow.next = None
m = self.reverseList(m)
while h is not None and m is not None:
p = m.next
m.next = h.next
h.next = m
h = h.next.next
m = p
return
# @lc code=end
# %%