def intersection_list(head1, head2): ll = LinkedList() cur1 = head1 cur2 = head2 while cur1 and cur2: if cur1.data < cur2.data: cur1 = cur1.next elif cur1.data > cur2.data: cur2 = cur2.next else: ll.insert_at_ending(cur2.data) cur1 = cur1.next cur2 = cur2.next return ll
new.next = cur1 cur1 = cur1.next new = new.next while cur2 : new.next = cur2 cur2 = cur2.next new = new.next new.next = None return head def insert_at_ending(last,node): if not last : return node else : last.next = node return node if __name__=='__main__': ll1 = LinkedList() for i in range(1,15): ll1.insert_at_ending(i) ll2 = LinkedList() for j in range(11,19): ll2.insert_at_ending(j) head = merge_sortedlists(ll2.head,ll1.head) current = head while current : print(current.data) current = current.next
current = head next = None prev = None count = 0 while current and count < k: next = current.next current.next = prev prev = current current = next count += 1 if next: head.next = current count = 0 while current and count < k - 1: current = current.next count += 1 if current: current.next = reverse_alt_knodes(current.next, k) return prev if __name__ == '__main__': ll = LinkedList() for i in range(1, 10): ll.insert_at_ending(i) ll.head = reverse_alt_knodes(ll.head, 3) ll.print_list()
def delete_nodes(head): if not head: return else: new_head = reverse(head) current = new_head _max = current.data while current and current.next: if current.next.data < _max and current.next: current.next = current.next.next else: _max = current.data current = current.next head1 = reverse(new_head) return head1 if __name__ == '__main__': ll = LinkedList() ll.insert_at_ending(12) ll.insert_at_ending(15) ll.insert_at_ending(10) ll.insert_at_ending(11) ll.insert_at_ending(5) ll.insert_at_ending(6) ll.insert_at_ending(2) ll.insert_at_ending(3) ll.head = delete_nodes(ll.head) ll.print_list()
cur1 = head1 cur2 = head2 while cur1 and cur2: if cur1.data != cur2.data: return False else: cur1 = cur1.next cur2 = cur2.next if cur1: return False if cur2: return False return True if __name__ == '__main__': ll = LinkedList() for i in range(1, 10): ll.insert_at_ending(i) l2 = LinkedList() for i in range(1, 10): l2.insert_at_ending(i) l3 = LinkedList() for j in range(1, 5): l3.insert_at_ending(j) print(check_identical(ll.head, l2.head)) print(check_identical(l2.head, l3.head)) print(is_identical(ll.head, l2.head)) print(is_identical(l2.head, l3.head))
cur1 = cur1.next else: current.next = cur2 cur2 = cur2.next current = current.next while cur1: current.next = cur1 cur1 = cur1.next current = current.next while cur2: current.next = cur2 cur2 = cur2.next current = current.next return head if __name__ == '__main__': l1 = LinkedList() for i in range(1, 10): l1.insert_at_ending(i) l2 = LinkedList() for i in range(1, 15, 2): l2.insert_at_ending(i) head = merge_two_ll(l1.head, l2.head) current = head while current: print(current.data) current = current.next
def addsamesize(head1, head2): if not head1 and not head2: return None, 0 else: node = Node() node.next, carry = addsamesize(head1.next, head2.next) _sum = head1.data + head2.data + carry carry = _sum // 10 _sum = _sum % 10 node.data = _sum return node, carry if __name__ == '__main__': l1 = LinkedList() l1.insert_at_ending(5) l1.insert_at_ending(6) l1.insert_at_ending(3) l1.insert_at_ending(3) l2 = LinkedList() l2.insert_at_ending(8) l2.insert_at_ending(4) l2.insert_at_ending(2) #head = add_numbers(l1.head,l2.head) #print_ll(head) root = addnumbers(l1.head, l2.head) print_ll(root)
prev.next = current.next current = prev.next if head.data == value: head = head.next return head def _remove_element(head, value): if not head: return else: current = head while current and current.next: if current.next.data != value: current = current.next else: current.next = current.next.next current = current.next if head.data == value: head = head.next return head if __name__ == '__main__': ll = LinkedList() ll.insert_at_ending(1) ll.insert_at_ending(1) ll.insert_at_ending(1) ll.insert_at_ending(1) _remove_element(ll.head, value=1) ll.print_list()