2021-04-27:python数组和链表:刷题206,141,142,25

python数据结构:

数组和链表:

2021-04-27:python数组和链表:刷题206,141,142,25_第1张图片

用内存管理器可以实现访问任何一个数组的位置

查找: O(1)的时间复杂度

2021-04-27:python数组和链表:刷题206,141,142,25_第2张图片

插入: O(N) 时间复杂度

如果只插入最后一个位置,就是O(1) 平均就是O(N/2)   等于O(N)

单链表:

2021-04-27:python数组和链表:刷题206,141,142,25_第3张图片

链表常见操作,插入和删除

 

双链表:

2021-04-27:python数组和链表:刷题206,141,142,25_第4张图片

插入和删除

2021-04-27:python数组和链表:刷题206,141,142,25_第5张图片

 

leetcode 206:

2021-04-27:python数组和链表:刷题206,141,142,25_第6张图片

解法1:自己的思路

# 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: ListNode) -> ListNode:
        curr, prev = head, None
        while curr:
            tmp, curr.next, prev= curr.next, prev, curr;
            curr =tmp;
        return prev;

解法2:更优解法

# 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: ListNode) -> ListNode:
        curr, prev = head, None
        while curr:
            curr.next, prev, curr = prev, curr, curr.next;
        return prev;

在 Python 批量连续赋值时,等号的右边为局部变量,不会因为等号左边值改变而改变

这个算法的核心就是这一行代码:

 cur.next, prev, cur = prev, cur, cur.next

假设当前 cur 指向 1,prev 指向 None,那个这个批量赋值语句的具体过程如下:

cur.next = prev(值为 None),prev = cur(值为 1),cur = cur.next(值为 2)。这里要注意的时,cur = cur.next 等号右边的 cur.next 为局部变量,保存着 2 而不是经过赋值后的 None。

2021-04-27:python数组和链表:刷题206,141,142,25_第7张图片

垃圾解法:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        m = {}
        while head:
            if m.get(head):
                return True
            m[head] = 1
            head = head.next
        return False

常量内存解法:

标记遍历过的节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        while head:
            if head.val == '1':
                return True
            head.val = '1'
            head = head.next
        return False

 

最佳解法:快慢指针:

2021-04-27:python数组和链表:刷题206,141,142,25_第8张图片

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: ListNode) -> ListNode:
        fast, slow = head, head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                fast = head
                while fast!=slow:
                    fast = fast.next
                    slow = slow.next
                return fast
        return  

最难链表:

2021-04-27:python数组和链表:刷题206,141,142,25_第9张图片

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        if head==None or k<2:
            return head
        dummy = ListNode()
        dummy.next = head
        start = dummy
        end = dummy.next
        count = 0
        while end:
            count = count+1
            if count%k ==0:
                start = self.reverse(start,end.next)
                end = start.next
            else:
                end = end.next
        return dummy.next
    
    def reverse(self,start,end):
        prev = start
        cur = start.next
        first = cur
        while cur != end:
            tmp = cur.next
            cur.next = prev
            prev = cur
            cur = tmp
        first.next = cur
        start.next = prev
        return first

 

 

你可能感兴趣的:(数据结构,leetcode,链表)