# encoding=utf-8 class Node(): def __init__(self, data,p=0): self.data = data self.next = p class linkList(): def __init__(self): self.length = 0 self.head = 0 ''' 判断链表是否为空 ''' def is_empty(self): return self.length == 0 ''' 链表长度 ''' def length(self): current = self.head if current: i = 1 while current.next: current = current.next i += 1 return i else: return 0 ''' 打印链表 ''' def print_link(self): if self.is_empty(): print('empty') else: node = self.head print('head',node.data) while node.next: node = node.next print(node.data) ''' 链表末尾添加node ''' def append_node(self, this_node): if self.is_empty(): ''' 链表为空,head指向node ''' self.head = this_node else: node = self.head while node.next: node = node.next node.next = this_node self.length += 1 ''' 链表插入 ''' def insertlink(self, value, index): if index > self.length: # 超出范围 直接return return else: # 获取当前node this_node = Node(data=value) # 获取head current = self.head if index == 0: self.head = this_node this_node.next = current return while index - 1: current = current.next index -= 1 print('insert is ',current.data) this_node.next = current.next current.next = this_node self.length += 1 return ''' 链表删除 ''' def delete_index(self,index): if index > self.length: return else: if index == 0: self.head = self.head.next else: current = self.head while index - 1: current = current.next index -= 1 print('delete is',current.data) current.next = current.next.next self.length -= 1 return ''' 链表修改 ''' def update_link(self,value,index): if index > self.length: return else: this_node = Node(data=value) if index == 0: this_node.next = self.head.next self.head = this_node else: current = self.head while index - 1: current = current.next index -= 1 this_node.next = current.next.next current.next = this_node def delete_final(self): index = self.length - 1 current = self.head while index - 1: current = current.next index -= 1 current.next = None self.length -= 1 return def clear(self): """ 清空链表 :return: None """ self.head = None self.length = 0 print("Clear the linked list finished.") def get_value(self, index): """ 获取链表中某个位置节点的值 :param index: 位置索引 :return: 该节点值, int or not """ if type(index) is int: if index > self.length: # 索引值超出范围直接提示并且退出 print("Index is out of range.") return else: if index == 0: return self.head.data else: current_node = self.head while index - 1: current_node = current_node.pnext index -= 1 return current_node.pnext.data else: print("Index value is not int.") return def is_huan(self):
fast = self.head
slow = self.head
if fast == None:
return False
while fast != None and fast.next != None:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return !(fast == None or fast.next == None) node1 = Node('111') node2 = Node('222') node3 = Node('333') node4 = Node('444') link = linkList() link.append_node(node1) link.append_node(node2) link.append_node(node3) link.append_node(node4) link.insertlink('666',3) link.print_link() link.update_link('888',3) link.print_link() link.delete_final() link.print_link()