def union(list1, list2):
    # Return other List if one of them is empty
    if (list1.is_empty()):
        return list2
    elif (list2.is_empty()):
        return list1

    unique_values = set()
    result = LinkedList()

    start = list1.get_head()

    # Traverse the first list till the tail
    while start:
        unique_values.add(start.data)
        start = start.next_element

    start = list2.get_head()

    # Traverse the second list till the tail
    while start:
        unique_values.add(start.data)
        start = start.next_element

    # Add elements of unique_vales to result
    for x in unique_values:
        result.insert_at_head(x)
    return result
Пример #2
0
def intersection(list1, list2):

    result = LinkedList()
    current_node = list1.get_head()

    # Traversing list1 and searching in list2
    # insert in result if the value exists
    while current_node is not None:
        value = current_node.data
        if list2.search(value) is not None:
            result.insert_at_head(value)
        current_node = current_node.next_element

    # Remove duplicates if any
    remove_duplicates(result)
    return result
def intersection(list1, list2):

    result = LinkedList()
    visited_nodes = set()  # Keep track of all the visited nodes
    current_node = list1.get_head()

    # Traversing list1 and adding all unique nodes into the hash set
    while current_node is not None:
        value = current_node.data
        if value not in visited_nodes:
            visited_nodes.add(value)  # Visiting current_node for first time
        current_node = current_node.next_element

    start = list2.get_head()

    # Traversing list 2
    # Nodes which are already present in visited_nodes are added to result
    while start is not None:
        value = start.data
        if value in visited_nodes:
            result.insert_at_head(start.data)
        start = start.next_element
    result.remove_duplicates()
    return result
    start = list2.get_head()

    # Traversing list 2
    # Nodes which are already present in visited_nodes are added to result
    while start is not None:
        value = start.data
        if value in visited_nodes:
            result.insert_at_head(start.data)
        start = start.next_element
    result.remove_duplicates()
    return result


ulist1 = LinkedList()
ulist2 = LinkedList()
ulist1.insert_at_head(8)
ulist1.insert_at_head(22)
ulist1.insert_at_head(15)

ulist2.insert_at_head(21)
ulist2.insert_at_head(14)
ulist2.insert_at_head(15)
ulist2.insert_at_head(7)

new_list = union(ulist1, ulist2)

new_list.print_list()

ilist1 = LinkedList()
ilist2 = LinkedList()
Пример #5
0
    nth_node = lst.get_head()  # This iterator will reach the Nth node
    end_node = lst.get_head()  # This iterator will reach the end of the list

    count = 0
    if not lst.is_empty():
        while count < n:
            if end_node is None:
                return -1
            end_node = end_node.next_element
            count += 1

    while end_node is not None:
        end_node = end_node.next_element
        nth_node = nth_node.next_element

    return nth_node.data


lst = LinkedList()
lst.insert_at_head(21)
lst.insert_at_head(14)
lst.insert_at_head(7)
lst.insert_at_head(8)
lst.insert_at_head(22)
lst.insert_at_head(15)

lst.print_list()
print(find_nth(lst, 5))
print(find_nth(lst, 1))
print(find_nth(lst, 10))
Пример #6
0
        if onestep == twostep:  # Loop exists
            return True
    return False


lst = LinkedList()
lst.insert_at_tail_myCode(6)
lst.insert_at_tail_myCode(4)
lst.insert_at_tail_myCode(9)
lst.insert_at_tail_myCode(10)
lst.print_list()

print(detect_loop(lst))

l2 = LinkedList()

l2.insert_at_head(21)
l2.insert_at_head(14)
l2.insert_at_head(7)

# Adding a loop
head = l2.get_head()
node = l2.get_head()

for i in range(4):
    if node.next_element is None:
        node.next_element = head.next_element
        break
    node = node.next_element

print(detect_loop(l2)) # note that we cant print the linkedlist as there is loop and it will run infinitely
Пример #7
0
    lst.head_node=current_node
    return lst

def reverse(lst):
  # To reverse linked, we need to keep track of three things
  previous = None # Maintain track of the previous node
  current = lst.get_head() # The current node
  next = None # The next node in the list

  #Reversal
  while current:
    next = current.next_element
    current.next_element = previous
    previous = current
    current = next

    #Set the last element as the new head node
    lst.head_node = previous
  return lst

lst = LinkedList()
lst.insert_at_head(6)
lst.insert_at_head(4)
lst.insert_at_head(9)
lst.insert_at_head(10)
lst.print_list()

reverse_myCode(lst)
lst.print_list()