def partition(head, x): if head == None: return p1 = head p2 = head after = None while p2 != None: if p2.get_data() > x: # copy to after and delete current if after == None: after = Node(p2.get_data()) else: after.append_to_tail(p2.get_data()) # if p2 is head if p2 == head: head = head.get_next() p1 = head p2 = head else: p1.set_next(p2.get_next()) p2 = p2.get_next() else: p1 = p2 p2 = p2.get_next() # append to end p1.set_next(after) return head
def add(self, item): """add(item) 链表头部添加元素""" node = Node(item) if self.is_empty(): self.head = node return node.next = self.head self.head = node
def test(self): head = Node(0) for i in range(1, 10): head.append_to_tail(i) head.print_all_data() self.assertEqual(find_kth_last_element2(3, head), 7) self.assertEqual(find_kth_last_element2(10, head), 0) self.assertEqual(find_kth_last_element2(12, head), None)
def add(self, item): """add(item) 链表头部添加元素""" node = Node(item) if self.is_empty(): self.head = node self.head.next = self.head return tmp_node = self.head while tmp_node.next != self.head: tmp_node = tmp_node.next node.next = self.head tmp_node.next = node self.head = node
def append(self, item): """ append(item) 链表尾部添加元素""" node = Node(item) # 空节点的时候 if self.is_empty(): self.head = node # 自己指向自己 self.head.next = self.head # 单节点的时候 elif self.head.next == self.head: self.head.next = node node.next = self.head else: tmp_node = self.head while tmp_node.next != self.head: tmp_node = tmp_node.next tmp_node.next = node node.next = self.head
def insert(self, pos, item): """insert(pos, item) 指定位置添加元素""" node = Node(item) if pos <= 1: self.add(node) elif pos >= self.length(): self.append(node) else: count = 1 pre_node = self.head next_node = self.head while next_node.next != self.head: pre_node = next_node next_node = next_node.next count += 1 if count == pos: break pre_node.next = node node.next = next_node
def sum_list_recursive(op1, op2, plus=False): if op1 == None and op2 == None and plus == False: return None res = None value = (1 if plus else 0) if op1 != None: value += op1.get_data() if op2 != None: value += op2.get_data() res = Node(value % 10) if value >= 10: plus = True else: plus = False # at least one list is not ended --> recurse if op1 != None or op2 != None: tail = sum_list_recursive((op1.get_next() if op1 != None else None), (op2.get_next() if op2 != None else None), plus) res.set_next(tail) return res
def append(self, item): """ append(item) 链表尾部添加元素""" node = Node(item) # 空节点的时候 if self.is_empty(): self.head = node # 单节点的时候 elif not self.head.next: self.head.next = node else: tmp_node = self.head while tmp_node.next: tmp_node = tmp_node.next tmp_node.next = node
p1 = head p2 = head.get_next().get_next() if p2 == None: return while p2 != None and p2.get_next() != None: p2 = p2.get_next().get_next() p1 = p1.get_next() p1.set_next(p1.get_next().get_next()) return head ## given only access to that node. def delete_middle(node): # copy the next node to current position, then delete next node.set_data(node.get_next().get_data()) node.set_next(node.get_next().get_next()) # if 'node' is the last one, mark as dummy #--------------- head = Node(0) for i in range(1, 9): head.append_to_tail(i) head.print_all_data() print '----------------' delete_middle(head) head.print_all_data()