def first_last_ordering_without_stack(head, reversed_head, middle): if not _check_parameters(head, reversed_head, middle): return t_head = head t_reversed_head = reversed_head count = 1 new_head = Node() new_head_pointer = new_head while t_head != middle: if count%2: new_head.next = Node(t_head.contents) new_head = new_head.next # if not new_head: # new_head = Node(t_head.contents) # else: # new_head.next = Node(t_head.contents) # new_head = new_head.next t_head = t_head.next else: # if not new_head: # new_head = Node(t_reversed_head.contents) # else: # new_head.next = Node(t_reversed_head.contents) # new_head = new_head.next new_head.next = Node(t_reversed_head.contents) new_head = new_head.next t_reversed_head = t_reversed_head.next count = count + 1 new_head.next = Node(t_head.contents) return new_head_pointer
def insert_node(head, prev, node): if prev == None: temp = Node(node.contents) temp.next = head else: temp = Node(node.contents) prev.next = temp temp.next = head return temp
def _simple_list_eg(): n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n5 head = n1 return head
def _not_complete_circular_eg(): n1 = Node(1) n2 = Node(2) n3 = Node(3) n4 = Node(4) n5 = Node(5) n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n5 n5.next = n2 head = n1 return head
def insert_in_front(head, diff): # insert extra nodes of zero in the front if head == None: return for i in range(diff): temp = Node(0) temp.next = head head = temp return head
def helper_insert_in_front_and_modify(node, head, prev): if not node: return node_next = node.next #insert in front temp = Node(node.contents) temp.next = head #make connection prev.next = node_next # delete node del node return temp
def insert_in_front(value, head): temp = Node(value) #if list is empty if head != None: temp.next = head return temp