力扣-138随机链表的复制+148.排序链表

刷力扣热题–第二十二天:138随机链表的复制+148.排序链表
菜鸟第二十二天开始奋战敲代码,持之以恒,见证成长

1.题目简介

(1)随机链表的复制
在这里插入图片描述

(2)排序链表
在这里插入图片描述

2.题目解答

(1)随机链表的复制
空间换时间,先复制每个链表中随机链表对应原始链表的位置,之后复制完成之后再对随机链表的指向进行更新即可.时间复杂度:O(N),空间复杂度O(N)
在这里插入图片描述

(2)排序链表
开始想的是利用头插法新建一个链表,每次循环就是将当前链表插入到新建的链表当中,时间复杂度O(N2),空间复杂度O(N)

超时了,现在想的就是借助于链表排序,之后再将结果一个一个的新建~时间复杂度O(NlogN),空间复杂度O(N)
在这里插入图片描述
这是下下策,因为耗的时间太长了,所以先做出来再说吧~

3.心得体会

题目不难,但积累太少~
(1)随机链表的复制

"""
# Definition for a Node.
class Node:
    def __init__(self, x, next=None, random=None):
        self.val = int(x)
        self.next = next
        self.random = random
"""

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        ori_head = head
        new_head = Node(0)
        return_head = new_head
        def get_random(head,node):
            index = 0
            while head:
                if node is None:
                    return None
                if node == head:
                    return index
                head = head.next
                index += 1
        while head:
            temp = Node(0)
            temp.val = head.val
            temp.next = head.next
            temp.random = get_random(ori_head,head.random)
            new_head.next = temp
            head = head.next
            new_head = new_head.next
        def find_random(head,num):
            if num is None:
                return None
            index = 0
            while head:
                if index == num:
                    return head
                head = head.next
                index += 1
        new_head = return_head.next
        while new_head:
            new_head.random = find_random(return_head.next,new_head.random)
            new_head = new_head.next
        return return_head.next

(2)排序链表
超时组

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None
        new_head = ListNode(head.val,None)
        head = head.next
        def sort_head(head,new_head):
            print(head,new_head)
            temp_head = ListNode()
            return_head = temp_head
            temp_head.next = new_head
            temp = new_head
            while temp:
                if head.val < temp.val:
                    temp_head.next = ListNode(head.val,temp)
                    return return_head.next
                temp_head = temp_head.next
                temp = temp.next
            # print(head,head.val,temp_head)
            temp_head.next = ListNode(head.val,None)
            # print(temp_head)
            return return_head.next                    
        while head:
            new_head = sort_head(head,new_head)
            head = head.next
        return new_head

使用数组替换组

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return None
        if head.next is None:
            return head
        list_num = []
        while head:
            list_num.append(head.val)
            head = head.next
        list_num.sort()
        temp_head = ListNode(list_num[0],ListNode(list_num[1],None))
        temp = temp_head.next
        for i in range(2,len(list_num)):
            temp.next = ListNode(list_num[i],None)
            temp = temp.next
        return temp_head

4.做题时长

7月29日
(1)随机链表的复制
14:05-14:33
(2)排序链表
14:35-14:56 15:15-15:48

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值