LeetCode刷题_143. Reorder List

原题链接:https://leetcode.com/problems/reorder-list/description/
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You may not modify the values in the list’s nodes, only nodes itself may be changed.

Example 1:

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

Example 2:

Given 1->2->3->4->5, reorder it to 1->5->2->4->3.

下面这种写法会超时,写得实在太菜了

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
import math
class Solution:
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: void Do not return anything, modify head in-place instead.
        """
        #(1)首先将链表复制一遍
        #(2)将新链表逆序
        #(3) 1 2 3 4 5
        #      5 4 3 2 1
        #循环size/2向上取整步,依次上下取node为新链表的node, 转换成新链表 1 5 2 4 3.
        
        current = head
        if head == None:
            new_head = None
            #pass
        else:
            new_head = ListNode(head.val)
            new_head.next = None
            size = 0
            while current:
                node = ListNode(current.val)
                index = new_head
                size += 1
                while index:
                    former = index
                    index = index.next
                former.next = node
                node.next = None
                current = current.next
        #将new_head链表逆序
        if new_head==None: 
            pass
            #return new_head
        else: 
            former = None 
            current = new_head 
            latter = new_head.next 
            while latter!=None: 
                current.next = former 
                former = current 
                current = latter 
                latter = latter.next 
            current.next = former 
            new_head = current
            
            #依次取上下
            former_1 = head
            latter_1 = head.next
            former_2 = new_head
            latter_2 = new_head.next
       
            for i in range(math.ceil(size/2)-1):
                former_1.next = former_2
                former_2.next = latter_1
               
                former_1  = latter_1      
                latter_1 = latter_1.next
                former_2 = latter_2
                latter_2 = latter_2.next
            if size%2 == 0:
                former_1.next = former_2
                former_2.next = None
            else:
                former_1.next = None
            #return head

主要是复制那儿有点问题,更改过后能A

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
import math
class Solution:
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: void Do not return anything, modify head in-place instead.
        """
        #(1)首先将链表复制一遍
        #(2)将新链表逆序
        #(3) 1 2 3 4 5
        #      5 4 3 2 1
        #循环size/2向上取整步,依次上下取node为新链表的node, 转换成新链表 1 5 2 4 3.
        
        
        #复制链表,但是原来的时间复杂度太高了,在复制这儿进行一下精简
        current = head
        if head == None:
            new_head = None
            #pass
        else:
            new_head = ListNode(head.val)
            new_head.next = None
            size = 0
            index = new_head
            while current:
                node = ListNode(current.val)      
                #index = new_head
                size += 1
                #while index:
                #    former = index
                #    index = index.next
                #former.next = node
                #node.next = None
                index.next = node
                node.next = None
                index = node
                current = current.next
        
        
        #将new_head链表逆序
        if new_head==None: 
            pass
            #return new_head
        else: 
            former = None 
            current = new_head 
            latter = new_head.next 
            while latter!=None: 
                current.next = former 
                former = current 
                current = latter 
                latter = latter.next 
            current.next = former 
            new_head = current
            
            #依次取上下
            former_1 = head
            latter_1 = head.next
            former_2 = new_head
            latter_2 = new_head.next
       
            for i in range(math.ceil(size/2)-1):
                former_1.next = former_2
                former_2.next = latter_1
                #if size%2 == 0:
                #    latter_1.next = latter_2 
                former_1  = latter_1      
                latter_1 = latter_1.next
                former_2 = latter_2
                latter_2 = latter_2.next
            if size%2 == 0:
                former_1.next = former_2
                former_2.next = None
            else:
                former_1.next = None
            #return head

你可能感兴趣的:(编程练习)