单链表
class Node:
def __init__(self, val):
self.val = val
self.next = None
class MyLinkedList(object):
def __init__(self):
self._head = Node(0)
self._count = 0
def get(self, index):
"""
:type index: int
:rtype: int
"""
cur_node = self._head
if 0 <= index < self._count:
for _ in range(index+1):
cur_node = cur_node.next
return cur_node.val
else:
return -1
def addAtHead(self, val):
"""
:type val: int
:rtype: None
"""
return self.addAtIndex(0,val)
def addAtTail(self, val):
"""
:type val: int
:rtype: None
"""
return self.addAtIndex(self._count,val)
def addAtIndex(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
if index < 0:
index = 0
if index > self._count:
return
add_node = Node(val)
prev_node, cur_node = None, self._head
for _ in range(index+1):
prev_node, cur_node = cur_node, cur_node.next
else:
prev_node.next = add_node
add_node.next = cur_node
self._count += 1
def deleteAtIndex(self, index):
"""
:type index: int
:rtype: None
"""
if 0<= index <self._count:
prev_node, cur_node = None, self._head
for _ in range(index+1):
prev_node, cur_node = cur_node, cur_node.next
else:
prev_node.next = cur_node.next
cur_node.next = None
self._count -= 1
else:
return
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
双链表
两个自己定义的功能函数:按 index 查找和在两个 Node 之间插入
功能函数插入的输入是两个 Node 而不使用 index,因为开头和结尾没必要查找
class Node(object):
def __init__(self, val):
self.val = val
self.prev = None
self.next = None
class MyLinkedList(object):
def __init__(self):
self._head = Node(0)
self._tail = Node(0)
self._head.next = self._tail
self._tail.prev = self._head
self._count = 0
def _get_node(self, index):
if index < self._count//2:
node = self._head
for _ in range(index+1):
node = node.next
else:
node = self._tail
for _ in range(self._count-index):
node = node.prev
return node
def get(self, index):
"""
:type index: int
:rtype: int
"""
if 0<= index < self._count:
node = self._get_node(index)
return node.val
else:
return -1
def _insert(self, prev, next, val):
add_node = Node(val)
prev.next = add_node
next.prev = add_node
add_node.prev, add_node.next = prev, next
self._count += 1
def addAtHead(self, val):
"""
:type val: int
:rtype: None
"""
return self._insert(self._head, self._head.next,val)
def addAtTail(self, val):
"""
:type val: int
:rtype: None
"""
return self._insert(self._tail.prev, self._tail, val)
def addAtIndex(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
if index < 0:
index = 0
if index > self._count:
return
node = self._get_node(index)
return self._insert(node.prev, node, val)
def deleteAtIndex(self, index):
"""
:type index: int
:rtype: None
"""
if 0<= index < self._count:
node = self._get_node(index)
node.prev.next = node.next
node.next.prev = node.prev
self._count -= 1
else:
return
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)