def deleteMax(head): head = reverse(head) printlist(head) maxval = -1*maxint curr = head prev = None while curr is not None and curr.next is not None: if curr.next.data < maxval: temp = curr.next curr.next = temp.next del temp else: curr = curr.next maxval = max(maxval, curr.data) head = reverse(head) return head
def sort(head): first_half = head while first_half.next is not None: if first_half.data > first_half.next.data: break first_half = first_half.next second_half = first_half.next first_half.next = None second_half.prev = None first_half = head printlist(first_half) printlist(second_half) second_half = reverse(second_half) printlist(second_half) return merge(first_half, second_half)
node_a.data, node_b.data = node_b.data, node_a.data def zigzagorder(head): flag = True curr = head while curr.next is not None: if flag and curr.data > curr.next.data: swap(curr, curr.next) if not flag and curr.data < curr.next.data: swap(curr, curr.next) flag = not flag curr = curr.next return head if __name__ == "__main__": res = [] start = time.time() for _ in xrange(1): size = random.randint(10, 15) nums = [random.randint(0, 100) for _ in xrange(size)] head = mklist(nums) printlist(head) head = zigzagorder(head) printlist(head)
def cutHalf(head): slowptr = head fastptr = head while fastptr.next != head and fastptr.next.next != head: slowptr = slowptr.next fastptr = fastptr.next.next if fastptr.next.next == head: fastptr = fastptr.next firsthead = slowptr.next slowptr.next = None fastptr.next = None return head, firsthead if __name__ == "__main__": head = tail = Node(10, None) for i in xrange(9, -1, -1): head = Node(i, head) tail.next = head printlist(head) head1, head2 = cutHalf(head) printlist(head1) printlist(head2)
def reverse(head): prev = None curr = head while curr is not None: next = curr.next curr.next = prev curr.prev = next prev = curr curr = next return prev if __name__ == "__main__": prev = None head = None for i in xrange(10, 0, -1): head = Node(i, prev, head) if prev is not None: prev.prev = head prev = head head.prev = None tail = printlist(head) head = reverse(head) printlist(head)
two = two.next return one is None and two is None if __name__ == "__main__": head_one = Node(0, None) for i in xrange(1,10): node = Node(i, head_one) head_one = node head_two = Node(0, None) for i in xrange(1,10): node = Node(i, head_two) head_two = node head_three = Node(0, None) for i in xrange(1,5): node = Node(i, head_three) head_three = node for i in xrange(1,5): node = Node(randint(1,100), head_three) head_three = node printlist(head_one) printlist(head_two) print("\nIs identical Itr, ", isIdenticalItr(head_one, head_two)) print("\nIs identical Rec, ", isIdenticalRec(head_one, head_two)) print("\nIs identical Itr, ", isIdenticalItr(head_one, head_three)) print("\nIs identical Rec, ", isIdenticalRec(head_one, head_three))