Python实现与LeetCode--链表
Python实现
Python实现单链表:
# @Time : 2019/7/1 8:55
# @Author : panky_pan
# @File : Linked_list.py
# @Software: PyCharm
# coding = utf-8
class Node(object):
"""
use to create node object
"""
def __init__(self, value=None, nxt=None):
self.value = value
self.next = nxt
def __str__(self): # return a string object
return str(self.value)
class LinkedList(object):
"""
use to make relationship between two nodes
Linked List:
node_1 --> node_2 --> node_3 --> node_4 --> None
"""
def __init__(self, iterable=()):
self.header = None
self.init(iterable)
def init(self, iterable=()):
# Note: use empty tuple rather than list to init iterable
if not iterable:
return
node = Node(iterable[0])
self.header = node
for i in iterable[1:]: # add all node
node.next = Node(i)
node = node.next
def __str__(self):
return " --> ".join(map(lambda x: str(x), self._traver_all()))
def _traver_all(self):
node = self.header
while node and node.next:
yield node
node = node.next
yield node
def show(self):
print(self)
@property
def length(self):
if self.header is None:
return 0
node = self.header # node pointer points to header
i = 1
while node.next:
node = node.next # node pointer move to the next
i += 1
return i
@property
def is_empty(self):
flag = False if self.header else True
return flag
def clear(self):
self.__init__()
def insert(self, index, item):
"""
example: node_1 --> node_2 --> node_3 --> node_4 --> None
insert(self, 2, 'v2')
node_1 --> node_2 --> node_insert --> node_3 --> node_4 --> None
:param item: the value of the node
:param index: abs(index) <= self.length
:return: None
"""
if abs(index) > self.length:
return "Out of index"
if index < 0:
self.insert(self.length + index + 1, item)
return
elif index == 0:
node_insert = Node(item)
node_insert.next = self.header
self.header = node_insert
return
node = self.header
i = 0
while i < index - 1:
node = node.next
i += 1
n = node.next
node_insert = Node(item, n)
node.next = node_insert
def append(self, item):
self.insert(self.length, item)
def prepend(self, item):
self.insert(0, item)
def find(self, item):
node = self.header
while node.next and node.value != item:
node = node.next
if node.value == item:
return node
return None
def delete(self, item):
prev = self.find_previous(item)
if prev:
prev.next = prev.next.next
def find_previous(self, item):
node = self.header
while node.next and node.next.value != item:
node = node.next
if node.next and node.next.value == item:
return node
return None
def test(linked_list):
print(linked_list)
print("\nis_empty test:")
print(linked_list.is_empty)
print("\nlength test:")
print("length: %s" % linked_list.length)
print("\ninsert test:")
linked_list.show()
linked_list.insert(2, 33)
linked_list.show()
linked_list.insert(-2, -22)
linked_list.show()
print("\nappend test:")
linked_list.append(66)
linked_list.show()
print("\nprepend test:")
linked_list.prepend(0)
linked_list.show()
print("\nfind test:")
print(linked_list.find(123))
print(linked_list.find(33))
print("\ndelete test:")
linked_list.show()
linked_list.delete(33)
linked_list.show()
print("\nclear test:")
linked_list.show()
linked_list.clear()
linked_list.show()
if __name__ == "__main__":
test(LinkedList((1, 2, 3, 4)))
LeetCode
反转链表
class Node(object):
"""
use to create node object
"""
def __init__(self, value=None, nxt=None):
self.value = value
self.next = nxt
def __str__(self): # return a string object
return str(self.value)
class LinkedList(object):
"""
use to make relationship between two nodes
Linked List:
node_1 --> node_2 --> node_3 --> node_4 --> None
"""
def __init__(self, iterable=()):
self.head = None
self.init(iterable)
def init(self, iterable=()):
# Note: use empty tuple rather than list to init iterable
if not iterable:
return
node = Node(iterable[0])
self.head = node
for i in iterable[1:]: # add all node
node.next = Node(i)
node = node.next
def __str__(self):
return " --> ".join(map(lambda x: str(x), self._traver_all()))
def _traver_all(self):
node = self.head
while node and node.next:
yield node
node = node.next
yield node
def show(self):
print(self)
def reverse_list(self):
cur, prev = self.head, None
while cur:
cur.next, prev, cur = prev, cur, cur.next
self.head = prev
return prev
def reverse_list2(self):
if self.head == None or self.head.next == None:
return self.head
# 声明两个指针和一个临时变量
p1 = self.head
p2 = p1.next
tmp = None
while p2:
# 两步:改变两结点的指针方向;两指针同时向后移动一个结点
tmp = p2.next
p2.next = p1
p1 = p2
p2 = tmp
self.head.next = None
self.head = p1
return p1
if __name__ == "__main__":
ll = LinkedList((1, 2, 3, 4))
ll.show()
ll.reverse_list()
ll.show()
ll.reverse_list2()
ll.show()