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
Пример #3
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)
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
    while current_node is not None:
        # Node to delete is found
        if value is current_node.data:
            # previous node now points to next node
            previous_node.next_element = current_node.next_element
            current_node.next_element = None
            deleted = True
            break
        previous_node = current_node
        current_node = current_node.next_element

    if deleted is False:
        print(str(value) + " is not in list!")
    else:
        print(str(value) + " deleted!")

    return deleted


l1 = LinkedList()
l1.insert_at_tail_myCode(0)
l1.insert_at_tail_myCode(1)
l1.insert_at_tail_myCode(6)
l1.insert_at_tail_myCode(9)
l1.print_list()
delete_myCode(l1, 6)
l1.print_list()
delete_myCode(l1, 6)
l1.print_list()
delete_myCode(l1, 1)
l1.print_list()
Пример #9
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))
Пример #10
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()
Пример #11
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()

Пример #12
0
from CodeBreakers.Data_structures_interview.LinkedList.LinkedList import LinkedList
from CodeBreakers.Data_structures_interview.LinkedList.Insertion_at_tail import insert_at_tail_myCode

#O(n) and space O(1)
def search(lst, value):
    head_pointer = lst.get_head()

    if head_pointer is None:
        return False

    while head_pointer:
        if head_pointer.data == value:
            return True
        head_pointer = head_pointer.next_element
    return False

L1=LinkedList()
insert_at_tail_myCode(L1,1)
insert_at_tail_myCode(L1,2)
L1.insert_at_tail_myCode(3)
L1.insert_at_tail_myCode(5)
L1.print_list()
print(search(L1,5))


Пример #13
0
    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


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

print("List 1")
ulist1.print_list()

ulist2.insert_at_head(7)
ulist2.insert_at_head(14)
ulist2.insert_at_head(8)

print("List 2")
ulist2.print_list()