n1.nxt = nxt2 pre1.nxt = n2 n2.nxt = nxt1 else: # somewhere in between pre1.nxt = n2 n2.nxt = nxt1 pre2.nxt = n1 n1.nxt = nxt2 return head if __name__ == '__main__': h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8]) h1 = swap_kth_beg_and_end(h, 1) print_ll(h1) print '' h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8]) h1 = swap_kth_beg_and_end(h, 2) print_ll(h1) print '' h = create_linked_list([1, 2, 3, 4, 5, 6, 7, 8]) h1 = swap_kth_beg_and_end(h, 3) print_ll(h1)
f = f.nxt if f is not None: s = s.nxt f = f.nxt front = head back = s.nxt s.nxt = None return front, back def merge_sort_ll(head): if head is None or head.nxt is None: return head a, b = split(head) a_result = merge_sort_ll(a) b_result = merge_sort_ll(b) head = sorted_merge(a_result, b_result) return head if __name__ == '__main__': h = create_linked_list([4, 5, 3, 1, 0, 6, 2]) h = merge_sort_ll(h) print_ll(h)
from G4G.Problems.linked_list.merge_sorted_ll import sorted_merge def merge_k_lists(linked_lists, last): while last != 0: i = 0 j = last while i < j: # check this recursive sorted merge function linked_lists[i] = sorted_merge(linked_lists[i], linked_lists[j]) i += 1 j -= 1 if i >= j: last = j return linked_lists[0] if __name__ == '__main__': a = create_linked_list([1, 3, 5]) b = create_linked_list([2, 4, 6, 8]) c = create_linked_list([0, 9, 10, 11]) res = merge_k_lists([a, b, c], 2) print_ll(res)
def modify_first_half(node, complement, n): if node is None: return complement else: first = modify_first_half(node.nxt, complement, n) if n[0] < n[1]: first.data = first.data - node.data n[0] += 1 return first.nxt def modify_list(head): cnt = count_nodes(head) n = [0, cnt // 2] modify_first_half(head, head, n) return head if __name__ == '__main__': h = create_linked_list([10, 4, 5, 3, 6]) modify_list(h) print_ll(h) h = create_linked_list([2, 9, 8, 12, 7, 10]) modify_list(h) print_ll(h)
def longest_palindrome_length(head): result = 0 prev = None curr = head while curr: nxt = curr.nxt curr.nxt = prev # check for odd length palindrome with # curr as middle result = max(result, 2 * count_common_nodes(prev, nxt) + 1) # check for even length palindrome result = max(result, 2 * count_common_nodes(curr, nxt)) prev = curr curr = nxt return result if __name__ == '__main__': h = create_linked_list([2, 3, 7, 3, 2, 12, 24]) print longest_palindrome_length(h) h = create_linked_list([12, 4, 4, 3, 14]) print longest_palindrome_length(h)
def get_number_from_rev_list(head): num = 0 multiplier = 1 while head is not None: num += (multiplier * head.data) multiplier *= 10 head = head.nxt return num if __name__ == '__main__': print get_number_from_rev_list(create_linked_list([1, 2, 3])) print get_number_from_list(create_linked_list([1, 2, 3]))[0] """ amzn, msft #tricky but good :) add num to ll or add 1 to ll """ def _add_num_to_linked_list(head, num): if head is not None: carry = _add_num_to_linked_list(head.nxt, num) op_sum = head.data + carry new_data = op_sum
t.nxt = head return h def add_lls(h1, h2): c1 = count_nodes(h1) c2 = count_nodes(h2) ll = h1 if c1 >= c2 else h2 sl = h1 if ll == h2 else h2 sl = append_zeros(sl, abs(c1 - c2)) carry = add_eq_len_lls(ll, sl) if carry > 0: head = Node(carry) head.nxt = h1 return head return h1 if __name__ == '__main__': l1 = create_linked_list([5, 6, 3]) l2 = create_linked_list([8, 4, 2]) _sum_list = add_lls(l1, l2) print_ll(_sum_list)
TAIL = TAIL.nxt def flatten_depth_wise(head): if head is None: return add_to_list(head) nxt = head.nxt flatten_depth_wise(head.down) flatten_depth_wise(nxt) if __name__ == '__main__': l1 = create_linked_list([1, 2, 3, 4], node=Node) n2 = l1.nxt n2.down = create_linked_list([7, 8, 10, 12], node=Node) l2 = n2.down l2.down = Node(9) l2.down.down = Node(14) l2.down.down.down = Node(15) l2.down.down.down.nxt = Node(23) l2.down.down.down.nxt.down = Node(24) l2n2 = l2.nxt l2n2.down = Node(16) l2n2.down.down = create_linked_list([17, 18, 19, 20], node=Node) l2n2.down.down.nxt.nxt.nxt.down = Node(21)
return counter def get_intersection_point(h1, h2): c1 = count_nodes(h1) c2 = count_nodes(h2) bl = h1 if c1 >= c2 else h2 sl = h1 if bl == h2 else h2 diff = abs(c1 - c2) while diff > 0: bl = bl.nxt diff -= 1 while bl is not None and sl is not None and bl != sl: bl = bl.nxt sl = sl.nxt return bl if __name__ == '__main__': l1 = create_linked_list([1, 2, 3, 4, 5, 6]) l2 = create_linked_list([8, 9]) l2.nxt.nxt = l1.nxt.nxt.nxt print get_intersection_point(l1, l2)
a = head b = head.nxt c = head.nxt.nxt if a.x() == b.x() == c.x(): a.nxt = c elif a.y() == b.y() == c.y(): a.nxt = c else: head = head.nxt return hd if __name__ == '__main__': h = create_linked_list([(0, 10), (1, 10), (5, 10), (7, 10), (7, 5), (20, 5), (40, 5)], node=Node) res = remove_mid_points(h) print_ll(res) print '' h = create_linked_list([(2, 3), (4, 3), (6, 3), (10, 3), (12, 3)], node=Node) res = remove_mid_points(h) print_ll(res)
b += carry if a >= b: h1.data = a - b carry = 0 else: a += 10 h1.data = a - b carry = 1 return carry def subtract_ll(h1, h2): a, b = get_subtract_able_lists(h1, h2) subtract_recur(a, b) return a if __name__ == '__main__': head1 = create_linked_list([1, 0, 0]) head2 = create_linked_list([1]) res = subtract_ll(head1, head2) print_ll(res) head1 = create_linked_list([7, 8, 6]) head2 = create_linked_list([7, 8, 9]) res = subtract_ll(head1, head2) print_ll(res)
while node is not None: clone_node = node.nxt node.nxt = node.nxt.nxt if clone_node.nxt: clone_node.nxt = clone_node.nxt.nxt node = node.nxt return cl_start if __name__ == '__main__': # with extra space ll1 = create_linked_list([1, 2, 3, 4], node=Node) ll1.other = ll1.nxt.nxt ll1.nxt.other = ll1.nxt.nxt.nxt ll1.nxt.nxt.other = ll1 ll1.nxt.nxt.nxt.other = ll1.nxt print_ll(ll1) ll2 = clone_without_extra_space(ll1) print_ll(ll2) print ll2.other print ll2.nxt.other print ll2.nxt.nxt.other print ll2.nxt.nxt.nxt.other print '' # without extra space
l1 = l1.nxt elif list1: tail.nxt = l1 tail = l1 l1 = l1.nxt else: tail.nxt = l2 tail = l2 l2 = l2.nxt list1 = not list1 return head if __name__ == '__main__': h1 = create_linked_list([5, 7, 17, 13, 11]) h2 = create_linked_list([12, 10, 2, 4, 6]) res = merge_alternate(h1, h2) print_ll(res) print '' h1 = create_linked_list([1, 2, 3]) h2 = create_linked_list([4, 5, 6, 7, 8]) res = merge_alternate(h1, h2) print_ll(res)
def sort_abs_val_sorted_ll(head): curr = head.nxt prev = head while curr is not None: if curr.data < prev.data: prev.nxt = curr.nxt curr.nxt = head head = curr curr = prev else: prev = curr curr = curr.nxt return head if __name__ == '__main__': h = create_linked_list([0, 1, -2, 3, 4, 5, -5]) sh = sort_abs_val_sorted_ll(h) print_ll(sh)
curr1 = curr1.nxt # initialization of result for the first time based on sum if pre1 == a and pre2 == b: result = pre1 if sum1 > sum2 else pre2 else: # further decisions based on sum if sum1 > sum2: pre2.nxt = pre1.nxt else: pre1.nxt = pre2.nxt pre1 = curr1 pre2 = curr2 if curr1 is not None: curr1 = curr1.nxt if curr2 is not None: curr2 = curr2.nxt if result: print_ll(result) if __name__ == '__main__': _a = create_linked_list([1, 3, 30, 90, 120, 240, 511]) _b = create_linked_list([0, 3, 12, 32, 90, 125, 240, 249]) get_max_list(_a, _b)
""" merge two sorted linked lists """ from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll def sorted_merge(a, b): if a is None: return b if b is None: return a if a.data > b.data: result = b result.nxt = sorted_merge(a, b.nxt) else: result = a result.nxt = sorted_merge(b, a.nxt) return result if __name__ == '__main__': l1 = create_linked_list([1, 3, 5, 7, 9]) l2 = create_linked_list([2, 4, 6, 8, 10]) r = sorted_merge(l1, l2) print_ll(r)
s.pop() if l % 2 != 0: b = b.nxt while b is not None: if b.data != s.pop().data: return False b = b.nxt return True if __name__ == '__main__': l = create_linked_list([1, 2, 2, 4, 4, 2, 2, 1]) print is_palindrome(l) l = create_linked_list([1, 2, 2, 3, 4, 4, 2, 2, 1]) print is_palindrome(l) l = create_linked_list([1, 2, 2, 4, 2, 2, 1]) print is_palindrome(l) l = create_linked_list([1, 2, 1]) print is_palindrome(l) l = create_linked_list([1, 1]) print is_palindrome(l) """ amzn, msft
a = la while a is not None: b = lb c = lc while b is not None and c is not None: _sum = a.data + b.data + c.data if _sum == sm: print 'Found', a.data, b.data, c.data return True elif _sum < sm: b = b.nxt else: c = c.nxt a = a.nxt return False if __name__ == '__main__': ha = create_linked_list([100, 15, 5, 20]) hb = create_linked_list([2, 4, 9, 10]) hc = create_linked_list([8, 4, 2, 1]) triplet_sum(25, ha, hb, hc)
""" Delete a node from an ll given access to only that node """ from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll, get_node def delete_node(node): if node.nxt: next_node = node.nxt node.data = next_node.data node.nxt = next_node.nxt next_node.nxt = None del next_node start = create_linked_list([1, 2, 3, 4, 5, 3, 2, 6, 7, 8, 9, 4, 7]) print_ll(start) node = get_node(start, 3) print node.data delete_node(node) print_ll(start)
second.nxt = first else: if pf: pf.nxt = second first.nxt = ns second.nxt = nf ps.nxt = first # return new head if head was swapped return second if head == first else head if __name__ == '__main__': h = create_linked_list([10, 15, 12, 13, 20, 14]) h = swap(h, 12, 20) print_ll(h) h = create_linked_list([10, 15, 12, 13, 20, 14]) h = swap(h, 10, 20) print_ll(h) h = create_linked_list([10, 15, 12, 13, 20, 14]) h = swap(h, 12, 13) print_ll(h) h = create_linked_list([10, 15, 12, 13, 20, 14]) h = swap(h, 12, 14) print_ll(h)
node.nxt = prev return prev def reverse2(node, prev): if node is None: return prev else: temp = reverse2(node.nxt, node) node.nxt = prev return temp if __name__ == '__main__': start = create_linked_list([1, 2, 3, 4, 5, 3, 2, 6, 7, 8, 9, 4, 7]) print_ll(start) new_head = reverse2(start, None) print_ll(new_head) """ Iterative reverse """ def iterative_reverse(head): prev = head curr = head.nxt prev.nxt = None
# update head ptr if head was moved if head == h: prev, h, nxt, t = move_node_to_end(prev, h, nxt, True, t) head = h else: prev, h, nxt, t = move_node_to_end(prev, h, nxt, False, t) else: prev = h h = nxt nxt = nxt.nxt return head if __name__ == '__main__': _h = create_linked_list([17, 15, 8, 12, 10, 5, 4, 1, 7, 6]) print_ll(segregate_odd_and_even(_h)) """ better approach """ def add_to_ll(head, tail, node): if head: tail.nxt = node return head, node return node, node def segregate_odd_and_even_another_app(head):
rh = rt = node return rh, rt def intersection_of_sorted_ll(h1, h2): rh = rt = None while h1 is not None and h2 is not None: if h1.data < h2.data: h1 = h1.nxt elif h1.data > h2.data: h2 = h2.nxt else: rh, rt = add_to_result(rh, rt, h1) h1 = h1.nxt h2 = h2.nxt rt.nxt = None return rh if __name__ == '__main__': a = create_linked_list([1, 2, 2, 3, 4, 6]) b = create_linked_list([2, 2, 4, 6, 8]) res = intersection_of_sorted_ll(a, b) print_ll(res)
# scan horizontally while True: if t.down: q.put(t.down) if t.nxt: t = t.nxt else: break return head if __name__ == '__main__': l1 = create_linked_list([10, 5, 12, 7, 11], Node) l2 = create_linked_list([4, 20, 13], Node) l3 = create_linked_list([17, 6], Node) l1.down = l2 l1.nxt.nxt.nxt.down = l3 l2.nxt.down = Node(2) l2.nxt.nxt.down = Node(16) l2.nxt.nxt.down.down = Node(3) l3.down = create_linked_list([9, 8], Node) l3.down.down = create_linked_list([19, 15], Node) h = flatten(l1)
h2 = b while b is not None and b.nxt is not None: a.nxt = a.nxt.nxt b.nxt = b.nxt.nxt a = a.nxt b = b.nxt a.nxt = None return h1, h2 if __name__ == '__main__': h = create_linked_list([0, 1, 0, 1, 0, 1]) _h1, _h2 = alternate_split(h) print_ll(_h1) print_ll(_h2) print '' h = create_linked_list([0, 1, 0, 1, 0]) _h1, _h2 = alternate_split(h) print_ll(_h1) print_ll(_h2) print '' h = create_linked_list([0, 1]) _h1, _h2 = alternate_split(h)
if new_head: new_tail.nxt = eqll_h else: new_head = eqll_h new_tail = eqll_t if gtll_h: if new_head: new_tail.nxt = gtll_h else: new_head = gtll_h return new_head if __name__ == '__main__': ll = create_linked_list([1, 4, 3, 2, 5, 2, 3]) res = partition_ll(ll, 3) print_ll(res) print '' ll = create_linked_list([1, 4, 2, 10]) res = partition_ll(ll, 3) print_ll(res) print '' ll = create_linked_list([10, 4, 20, 10, 3]) res = partition_ll(ll, 3) print_ll(res)
""" http://www.geeksforgeeks.org/rotate-a-linked-list/ """ from G4G.Problems.linked_list.linked_list import create_linked_list, print_ll from G4G.Problems.linked_list.segregate_odd_and_even_in_ll import get_tail_ll def rotate_ll(head, k): # converting to circular ll temporarily tail = get_tail_ll(head) tail.nxt = head while k > 0: tail = head head = head.nxt k -= 1 tail.nxt = None return head if __name__ == '__main__': h = create_linked_list([1, 2, 3, 4, 5, 6]) n = rotate_ll(h, 9) print_ll(n)
while head1 is not None and head2 is not None: if head1.data <= head2.data: node = head1 head1 = head1.nxt head = add_to_list(node, head) else: node = head2 head2 = head2.nxt head = add_to_list(node, head) while head1 is not None: node = head1 head1 = head1.nxt head = add_to_list(node, head) while head2 is not None: node = head2 head2 = head2.nxt head = add_to_list(node, head) return head if __name__ == '__main__': h1 = create_linked_list([5, 10, 15, 40]) h2 = create_linked_list([2, 3, 20]) h = merge_sorted_lls(h1, h2) print_ll(h)
if t1: t1.nxt = None if t2: t2.nxt = None return l1, l2 def split_and_reverse(head): h1, h2 = split_alternate_nodes(head) h2_rev = reverse2(h2, None) t = h1 while t.nxt is not None: t = t.nxt t.nxt = h2_rev return h1 if __name__ == '__main__': l = create_linked_list([1, 2, 3, 4, 5, 6]) res = split_and_reverse(l) print_ll(res) l = create_linked_list([12, 14, 16, 18, 20]) res = split_and_reverse(l) print_ll(res)