def insert_node_by_order(cls, head, num): node = Node(num) if head is None: node.next = node return node pre = None cur = head while True: if head.next == head: tail = head break if pre is not None and pre.value > cur.value: tail = pre break pre = cur cur = cur.next pre = None cur = head while True: if pre is not None and pre.value > cur.value: break if cur.value > node.value: if pre is not None: pre.next = node node.next = cur return head else: node.next = cur tail.next = node return node pre = cur cur = cur.next pre.next = node node.next = head return head
if cur1 is None: break while cur2 is not None and cur1.value > cur2.value: pre = cur2 cur2 = cur2.next pre.next = cur1 if cur2 is None: break if head1.value < head2.value: return head1 else: return head2 if __name__ == '__main__': head1 = Node(0) head1.next = Node(2) head1.next.next = Node(3) head1.next.next.next = Node(7) head2 = Node(1) head2.next = Node(3) head2.next.next = Node(5) head2.next.next.next = Node(7) head2.next.next.next.next = Node(9) new_head = MergeListTool.merge(head1, head2) MergeListTool.print_list(new_head)
if reversed_list1 is not None: reversed_list1 = reversed_list1.next if reversed_list2 is not None: reversed_list2 = reversed_list2.next if flag == 1: new_list.next = Node(1) reversed_new_head = ListAddTool.revert_linked_list(new_head) return reversed_new_head @staticmethod def revert_linked_list(head): pre = None while head is not None: next = head.next head.next = pre pre = head head = next return pre if __name__ == '__main__': node1 = Node(9) node1.next = Node(9) node1.next.next = Node(9) node2 = Node(1) ListAddTool.print_list(ListAddTool.add_list(node1, node2))
if new_pre_node is not None and count == 1: new_pre_node.next = temp_node pre_node = temp_node count -= 1 new_pre_node = first_node cur = next if len(temp_stack) > 0: temp_node = temp_stack.pop(0) if new_pre_node is not None: new_pre_node.next = temp_node else: return temp_node return new_head if __name__ == '__main__': head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5) head.next.next.next.next.next = Node(6) head.next.next.next.next.next.next = Node(7) head.next.next.next.next.next.next.next = Node(8) k = 3 ReversePartList.print_list(ReversePartList.reverse_part(head, k))
return head pre = None cur = head new_head = head while cur is not None: next = cur.next if cur.value == num: if pre is not None: pre.next = next else: new_head = cur.next cur.next = None del cur else: pre = cur cur = next return new_head if __name__ == '__main__': node = Node(1) node.next = Node(2) node.next.next = Node(3) node.next.next.next = Node(2) node.next.next.next.next = Node(4) RemoveAssignedNode.print_list(RemoveAssignedNode.remove_node(node, 2))
little_pre = None little = head while cur is not None: if cur.value < little.value: little_pre = pre little = cur pre = cur cur = cur.next if little != head: little_pre.next = little.next little.next = head return little if __name__ == '__main__': head = Node(3) head.next = Node(1) head.next.next = Node(2) head = SortList.sort_list(head) SortList.sort_list(head) SortList.print_list(head) head = Node(3) head.next = Node(1) head.next.next = Node(4) head.next.next.next = Node(2) head = SortList.sort_list(head) SortList.print_list(head)
if left_head is not None: if pivot_head is not None: left.next = pivot_head pivot_head.next = right_head else: left.next = right_head if right.next is not None: right.next = None return left_head else: if pivot_head is not None: pivot.next = right_head if right.next is not None: right.next = None return pivot_head else: return right_head if __name__ == '__main__': node = Node(9) node.next = Node(0) node.next.next = Node(4) node.next.next.next = Node(5) node.next.next.next.next = Node(1) partion_node = ListPartion.strict_partion(node, 4) ListPartion.print_list(partion_node)