美文网首页
143. Reorder List

143. Reorder List

作者: April63 | 来源:发表于2018-06-19 21:53 被阅读0次

实在是太困了 看不下去这一题的指针,明早上看

# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def reorderList(self, head):
        if not head:
            return
        
    # find the mid point
        slow = fast = head 
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

    # reverse the second half in-place
        pre, node = None, slow
        while node:
            pre, node.next, node = node, pre, node.next
    
   # Merge in-place; Note : the last node of "first" and "second" are the same
        first, second = head, pre
        while second.next:
            first.next, first = second, first.next
            second.next, second = first, second.next
        return 

相关文章

  • 143. Reorder List

    题目143. Reorder List Given a singly linked list L: L0→L1→…...

  • 143. Reorder List

    实在是太困了 看不下去这一题的指针,明早上看

  • 143. Reorder List

    题目分析 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorde...

  • 143. Reorder List

    题目 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder ...

  • 143. Reorder List

    分为以下几个步骤,1使用快慢指针找到中间节点,2翻转后半部分,3逐个插入节点。

  • 143. Reorder List

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it ...

  • 143. Reorder List

    题目描述:给链表如L: L0→L1→…→Ln-1→Ln,将其重新排序为L0→Ln→L1→Ln-1→L2→Ln-2→...

  • 143. Reorder List

    这题很简单,不过很容易出bug特别在merge的时候,所以一定要逻辑清楚,先写好框架

  • 143. Reorder List

    Example 1: Given 1->2->3->4, reorder it to 1->4->2->3.Exa...

  • 143. Reorder List

    重排链表L0 → L1 → … → Ln - 1 → Ln重写为L0 → Ln → L1 → Ln - 1 → L...

网友评论

      本文标题:143. Reorder List

      本文链接:https://www.haomeiwen.com/subject/hdpzeftx.html