Esempio n. 1
0
def add_two_numbers(head1, head2):
    result = l.S_List()
    p1 = head1
    p2 = head2
    carry = 0

    while (p1 is not None) and (p2 is not None):
        sum = p1.data + p2.data + carry
        if sum >= 10:
            sum -= 10
            carry = 1
        else:
            carry = 0

        node = l.S_Node(sum)
        result.insert(node)
        p1 = p1.next
        p2 = p2.next

    current = result.head
    while current.next is not None:
        current = current.next
    if p1 is not None:
        if carry == 1:
            p1.data += 1
        current.next = p1
    elif p2 is not None:
        if carry == 1:
            p2.data += 1
        current.next = p2

    return result
Esempio n. 2
0
            previous.next = p2
            previous = p2
            p2 = p2.next
            previous.next = p1
        if p1 is not None and p2 is None:
            previous.next = p1
        return new_head


#Review:
import linked_list as l
# Input: 1->2->4, 1->3->4
# Output: 1->1->2->3->4->4
list1 = [1, 2, 4]
list2 = [1, 3, 4]
input1 = l.S_List()
input2 = l.S_List()

for elem in list1:
    node = l.S_Node(elem)
    input1.insert(node)
for elem in list2:
    node = l.S_Node(elem)
    input2.insert(node)
result = merge_two_sorted_lists(input1.head, input2.head)
current = result
while current is not None:
    print(current.data)
    current = current.next
#list1 is shorter
# Input: 1, 1->3->4
Esempio n. 3
0
    return result


# edge case:
# Input: 2 -> 4 -> 3,  5 -> 6 -> 4
# Output: 7 -> 0 -> 8

# Input: 2 -> 4,  5 -> 6 -> 4
# Output: 7 -> 0 -> 5

# Input: 1 -> 2, 0
# Output: 1 -> 2
#review
input1 = [1, 2]
input2 = [0]
test1 = l.S_List()
test2 = l.S_List()

for elem in input1:
    node = l.S_Node(elem)
    test1.insert(node)

for elem in input2:
    node = l.S_Node(elem)
    test2.insert(node)

print('test1')
test1.print_list()
print('test2')
test2.print_list()
print('result')
Esempio n. 4
0
        return
    count = 0
    pre = None
    current = head
    while count < (length - n):
        pre = current
        current = current.next
        count += 1
    current = current.next
    pre.next = current
    return head


#Review
import linked_list as l
test = l.S_List()
input1 = [1, 2, 3, 4, 5]
n = 2
for elem in input1:
    node = l.S_Node(elem)
    test.insert(node)
print('Before remove')
test.print_list()
print('After remove')
result = remove_nth_node_from_end(test.head, n)
test.print_list()

#Evaluate
#Runtime = O(n)
#Space = O(1)
Esempio n. 5
0
        p2 = head.next
        pre = None
        while (p1 is not None) and (p2 is not None):
            if p1 is head:
                head = p2
            pre = swap(pre, p1, p2)
            p1 = p1.next
            if p1 is not None:
                p2 = p1.next
    return head


#review
import linked_list as l
test1 = [1, 2, 3, 4]
test_list = l.S_List()
for elem in test1:
    node = l.S_Node(elem)
    test_list.insert(node)
print('*** Before swap ***')
test_list.print_list()
print('*** After swap ***')
test_list.head = swap_nodes_in_pairs(test_list.head)
test_list.print_list()

#evaluate
#runtime: O(n)
#space: O(1)

#*************************
#. total time: 38m
Esempio n. 6
0
  id_set.add(id(current))
  current = current.next
  set_size = len(id_set)
  while current is not None and current is not head:
    id_set.add(id(current))
    if set_size == len(id_set):#no change in set set_size
      return True
    current = current.next
    
  if current is None:
    return False
  else:
    return True

test = [1,2,3,4,5]
test_list1 = l.S_List()
for elem in test:
  node = l.S_Node(elem)
  test_list1.insert(node)
print(detect_cycle(test_list1.head), '| Expected: False')

test_list = l.S_List()
print(detect_cycle(test_list.head), '| Expected: False')

test_list = l.S_Cycle_List()
for elem in test:
  node = l.S_Node(elem)
  test_list.insert(node)
print(detect_cycle(test_list.head), '| Expected: True')

#evaluate