链表 队列
索性将两者放在一起
列表转化为链表,链表转化为列表。测试集的转化和结果的输出,方便调试
这一部分还是有一些疑问,还需多多回顾。
"""https://leetcode.com/tag/linked-list/"""
# # Definition for singly-linked list.
# 定义链表
# class LinkedList:
#
# def __init__(self):
# self.head_, self.tail_ = None, None
#
# def isEmpty(self):
# return self.head_ is None
#
# def append(self, value):
# # 添加元素到链表最后
# node = ListNode(value)
# if self.isEmpty():
# self.head_ = node
# self.tail_ = node
# else:
# self.head_.next = node
# self.tail_ = node
#
# def iter(self):
# # 链表的显示
# if self.isEmpty(): return "Empty"
# res = []
# current = self.head_
# # print(current.val)
# res.append(str(current.val)+ ' -> ')
# # yield current.val
# while current.next:
# current = current.next
# # print(current.val, " ")
# res.append(str(current.val) + ' -> ')
# # yield current.val
# print( "".join(res))
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class LinkedList_Solution:
# 链表->列表
def InputtoList(self, head: ListNode):
res = []
while head.next:
res.append(head.val)
head = head.next
res.append(head.val)
return res
# 1290. Convert Binary Number in a Linked List to Integer
def getDecimaValue(self, head: ListNode):
print(head)
value = 0
while head:
value = 2 * value + head.val
head = head.next
return value
# 876. Middle of the Linked List
def middleNode(self, head:ListNode):
# 链表转换为列表,不推荐
# A = [head]
# while A[-1].next:
# print(A[-1].next)
# A.append(A[-1].next)
# return A[len(A) // 2]
# 双指针,快的是慢的两倍速
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# 876. 计数的方法
def middleNode_fast(self, head):
n = 1
node = head
while node.next != None:
node = node.next
n += 1
print(self.InputtoList(node))
if n % 2 == 0: # even
n = n / 2 + 1
else: # odd
n = n / 2 + 0.5
node = head
for i in range(int(n - 1)):
print(self.InputtoList(node))
node = node.next
return node
# 206. Reverse Linked List
def reverseList(self, head:ListNode)->ListNode:
# 类似数组中的交换,当前指针存储好链表顺序,再逆序输出
rervesion = None
while head:
current = head
head = head.next
current.next = rervesion
rervesion = current
return rervesion
# 237. Delete Node in a Linked List
def deleteNode(self, node):
# 删除一个节点要知道前一个节点和后一个节点。而用改数组的方式,并不是删除节点的含义
if node.next is None: return
node.val = node.next.val
node.next = node.next.next
# 21 Merge Two Sorted Lists
def mergeTwoLists(self, l1:ListNode, l2:ListNode):
if l1 is None: return l2
if l2 is None: return l1
if l1.val <= l2.val:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
else:
l2.next = self.mergeTwoLists(l1, l2.next)
return l2
# 83 Remove Dupicates from Sorted List
def deleteDuplicates(self, head:ListNode):
prev= head
if head.next:
cur = head.next
while cur:
if prev.val == cur.val:
prev.next = cur.next
cur = cur.next
else:
cur = cur.next
prev = prev.next
return head
# 列表转换为链表
def InputToListNode(nums):
# Now convert that list into linked list
dummyRoot = ListNode(0)
ptr = dummyRoot
for number in nums:
ptr.next = ListNode(number)
ptr = ptr.next
ptr = dummyRoot.next
# print(ptr.val)
return ptr
from collections import Counter
from heapq import heappush, heappop
# 641 Design Circular Deque
class MyCircularDeque:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the deque to be k.
"""
self.cique = [-1]*k
self.size, self.capicity = 0, k
self.front_, self.rear_ = 0, 0
def insertFront(self, value: int) -> bool:
"""
Adds an item at the front of Deque. Return true if the operation is successful.
"""
if self.isFull(): return False
if self.isEmpty(): self.cique[self.front_] = value
else:
self.front_ = (self.front_-1) % self.capicity # 数组最后一个
# self.front_ = self.capicity-1
self.cique[self.front_] = value
self.size += 1
return True
def insertLast(self, value: int) -> bool:
"""
Adds an item at the rear of Deque. Return true if the operation is successful.
"""
if self.isFull(): return False
if self.isEmpty(): self.cique[self.rear_] = value
else:
self.rear_ = (self.rear_+1) % self.capicity # 末尾
#self.rear_ = self.rear_ +1
self.cique[self.rear_] = value
self.size += 1
return True
def deleteFront(self) -> bool:
"""
Deletes an item from the front of Deque. Return true if the operation is successful.
"""
if self.isEmpty(): return False
self.cique[self.front_] = -1
self.front_ = (self.front_ + 1) % self.capicity # 变为0
self.size -= 1
if self.isEmpty():
self.rear_ = self.front_
return True
def deleteLast(self) -> bool:
"""
Deletes an item from the rear of Deque. Return true if the operation is successful.
"""
if self.isEmpty(): return False
self.cique[self.rear_] = -1
self.rear_ = (self.rear_ - 1) % self.capicity
self.size -= 1
if self.isEmpty():
self.front_ = self.rear_
return True
def getFront(self) -> int:
"""
Get the front item from the deque.
"""
return self.cique[self.front_]
def getRear(self) -> int:
"""
Get the last item from the deque.
"""
return self.cique[self.rear_]
def isEmpty(self) -> bool:
"""
Checks whether the circular deque is empty or not.
"""
return self.size == 0
def isFull(self) -> bool:
"""
Checks whether the circular deque is full or not.
"""
if self.capicity == self.size:
return True
else:
return False
# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()
def leastInterval(tasks, n):
curr, h = 0, []
for k, v in Counter(tasks).items():
heappush(h)
pass
if __name__ == '__main__':
so = LinkedList_Solution()
# a = ListNode(3)
# print(a.val, a.next)
# nums = InputToListNode([1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0]) # 1290 ans =18880
nums = InputToListNode([1,1,2,3,3]) # 876. #206 [1,2,3,4,5,6]
# n1 = InputToListNode([1,2,4]) # 83 [1,1,2,3,3]
# n2 =InputToListNode([1,3,4]) # 21
n = so.deleteDuplicates(nums)
print(so.InputtoList(n))