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 __init__(self, vertices):
     # Total number of vertices
     self.vertices = vertices
     # definining a list which can hold multiple LinkedLists
     # equal to the number of vertices in the graph
     self.array = []
     # Creating a new Linked List for each vertex/index of the list
     for i in range(vertices):
         temp = LinkedList()
         self.array.append(temp)
Пример #3
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
def find_mid(lst):
    if lst.is_empty():
        return -1
    current_node = lst.get_head()
    if current_node.next_element is None:
        # Only 1 element exist in array so return its value.
        return current_node.data

    mid_node = current_node
    current_node = current_node.next_element.next_element
    # Move mid_node (Slower) one step at a time
    # Move current_node (Faster) two steps at a time
    # When current_node reaches at end, mid_node will be at the middle of List
    while current_node:
        mid_node = mid_node.next_element
        current_node = current_node.next_element
        if current_node:
            current_node = current_node.next_element
    if mid_node:
        return mid_node.data
    return -1


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(find_mid_myCode(lst))
Пример #6
0

def insert_at_tail(lst, value):
    # Creating a new node
    new_node = Node(value)

    # Check if the list is empty, if it is simply point head to new node
    if lst.get_head() is None:
        lst.head_node = new_node
        return

    # if list not empty, traverse the list to the last node
    temp = lst.get_head()

    while temp.next_element:
        temp = temp.next_element

    # Set the nextElement of the previous node to new node
    temp.next_element = new_node
    return


l1 = LinkedList()
insert_at_tail_myCode(l1, 0)
l1.print_list()
insert_at_tail_myCode(l1, 1)
l1.print_list()
insert_at_tail_myCode(l1, 2)
l1.print_list()
insert_at_tail_myCode(l1, 3)
l1.print_list()
        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


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()
Пример #8
0
from CodeBreakers.Data_structures_interview.LinkedList.LinkedList import LinkedList

def detect_loop(lst):
    onestep = lst.get_head()
    twostep = lst.get_head()
    while onestep and twostep and twostep.next_element:
        onestep = onestep.next_element  # Moves one node at a time
        twostep = twostep.next_element.next_element  # skips a node
        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()