10.20 每日一题 143. 重排链表

给定一个单链表 L:L0→L1→…→Ln-1→Ln
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.

示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

通过次数47,851 | 提交次数82,512
代码实现

# 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 or not head.next:
            return
        fast,slow = head, head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        p,right = slow.next,None
        slow.next = None
        while p:
            right, right.next, p = p, right, p.next
        cur = head
        while  cur and right:
            cur.next, right.next, cur, right = right, cur.next, cur.next, right.next
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reorder-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

你可能感兴趣的:(leetcode,leetcode,链表,python)