def part(pivot): linked1 = None linked2 = None temp = head while temp != None: newNode = node(temp.data) if temp.data < pivot: if linked1 == None: linked1 = newNode else: linked1.appendToLast(newNode) else: if linked2 == None: linked2 = newNode else: linked2.appendToLast(newNode) temp = temp.next linked1.appendToLast(linked2) return linked1
from LinkedListNode import node l = [3, 5, 8, 5, 10, 2, 1] head = node(l[0]) for i in range(1, len(l)): head.appendToLast(node(l[i])) def part(pivot): linked1 = None linked2 = None temp = head while temp != None: newNode = node(temp.data) if temp.data < pivot: if linked1 == None: linked1 = newNode else: linked1.appendToLast(newNode) else: if linked2 == None: linked2 = newNode else: linked2.appendToLast(newNode) temp = temp.next linked1.appendToLast(linked2) return linked1 node = part(5) node.printAll()
if single_hop_head == double_hop_head: single_hop_head = single_hop_head.next_node start_of_loop = head_node while single_hop_head != start_of_loop: if single_hop_head == double_hop_head: start_of_loop = start_of_loop.next_node single_hop_head = single_hop_head.next_node else: single_hop_head = single_hop_head.next_node return start_of_loop if __name__ == '__main__': nc_node2, nc_node3, nc_node4, nc_node5, nc_node6 = \ node(data=2), node(data=3), node(data=4), node(data=5), node(data=6) c_node2, c_node3, c_node4, c_node5, c_node6 = \ node(data=2), node(data=3), node(data=4), node(data=5), node(data=6) non_cylical_head, cyclical_head = node(data=1, next_node=nc_node2), node( data=1, next_node=c_node2) nc_node2.next_node, nc_node3.next_node, nc_node4.next_node, nc_node5.next_node = \ nc_node3, nc_node4, nc_node5, nc_node6 c_node2.next_node, c_node3.next_node, c_node4.next_node, c_node5.next_node, c_node6.next_node = \ c_node3, c_node4, c_node5, c_node6, c_node2 # print(test_for_cycle(cyclical_head).data) assert (test_for_cycle(non_cylical_head) is None and test_for_cycle(cyclical_head) == c_node2)
current_node = head_node while current_node.next_node is not None and current_node.next_node.data != key: current_node = current_node.next_node if current_node.next_node is not None: current_node.next_node = current_node.next_node.next_node def delete_node_after(lead_node): if lead_node.next_node is not None: lead_node.next_node = lead_node.next_node.next_node if __name__ == '__main__': # test insert first_node, second_node, third_node, fourth_node = \ node(data=20), node(data=25), node(data=30), node(data=40) insert_node(first_node, second_node) insert_node(second_node, third_node) assert (first_node.next_node.data == 25) assert (second_node.next_node.data == 30) insert_node(second_node, fourth_node) assert (first_node.next_node.data == 25) assert (second_node.next_node.data == 40) assert (fourth_node.next_node.data == 30) # test search assert (search_for_key(first_node, 40) == fourth_node) assert (search_for_key(first_node, 50) is None)
from LinkedListNode import node l = [7, 1, 6] head1 = node(l[0]) for i in range(1, len(l)): head1.appendToLast(node(l[i])) l = [5, 9, 2] head2 = node(l[0]) for i in range(1, len(l)): head2.appendToLast(node(l[i])) n1 = 0 n2 = 0 count = 0 while head1 != None: n1 += head1.data * 10**count count += 1 head1 = head1.next count = 0 while head2 != None: n2 += head2.data * 10**count count += 1 head2 = head2.next n = n1 + n2 head = None temp = head