예제 #1
0
def main():
    ########## testing single linked list
    single_linked_list = SingleyLinkedList(Node(1))
    single_linked_list.add_node(Node(2))
    single_linked_list.add_node(Node(3))
    single_linked_list.add_node(Node(4))
    # single_linked_list.print()
    # print(single_linked_list.tail.get_value())
    # print(single_linked_list.cycle_check())
    # single_linked_list.tail.next_node=single_linked_list.tail
    # print(single_linked_list.cycle_check())
    # single_linked_list.reverse_list()
    #single_linked_list.print()
    print(single_linked_list.nth_to_last_node(1))
    single_linked_list.print()
    print(single_linked_list.nth_to_last_node(3))
예제 #2
0
def remove_duplicates_sorted_ii(llist):

    sentinel = Node(0, llist.head)
    
    pred = sentinel
    
    head = llist.head
    while head:
        
        if head.next and head.val == head.next.val:
            while head.next and head.val == head.next.val:
                head = head.next
            
            pred.next = head.next
        else:
            pred = pred.next
        
        head = head.next
    
    llist.head = sentinel.next
    return
예제 #3
0

def intersects(ll1, ll2):
    if ll1.getHead() == None or ll2.getHead() == None:
        return False
    current = ll1.getHead()
    aux = {}
    while current != None:
        aux[current] = current
        current = current.get_next()
    current = ll2.getHead()
    while current != None:
        if current in aux.keys():
            return True
        current = current.get_next()
    return False


ll1 = SinglyLinkedList()
inter = Node(5)
ll1.insert_node_at_beginning(inter)
ll1.insert_at_beginning(5)
ll1.insert_at_beginning(6)

ll2 = SinglyLinkedList()
ll2.insert_at_beginning(5)
ll2.insert_at_beginning(3)
ll2.insert_at_beginning(4)

print(intersects(ll1, ll2))
예제 #4
0
        self.first = new_node
 
    def remove(self, node):
        if self.first.next == self.first:
            self.first = None
        else:
            node.prev.next = node.next
            node.next.prev = node.prev
            if self.first == node:
                self.first = node.next
    def display(self):
        if self.first is None:
            return
        current = self.first
        while True:
            print(current.data, end = ' ')
            current = current.next
            if current == self.first:
                break  

list = CircularDoublyLinkedList()
n1 = Node("Melanie")
n2 = Node("Andrew")
n3 = Node("Ronaldo")
n4 = Node("Mister")
list.insert_at_beg(n1)
list.insert_at_beg(n2)
list.insert_at_end(n4)
list.insert_at_end(n3)
list.display()
예제 #5
0
	slow = linkedList._head
	fast = linkedList._head
	while slow is not None and slow._next is not None:
		slow = slow._next
		if fast is not None: 
			fast = fast._next
			fast = fast._next
		else: 
			return False 
		if slow == fast:
			return True
	return False


ll = SinglyLinkedList()
n5 = Node(5)
n4 = Node(4, n5)
n3 = Node(3, n4)
n2 = Node(2, n3)
n1 = Node(1, n2)

n5._next = n3

# setting the head to the created linked list
ll._head = n1

# testing to see if the linked list loops
curr = ll._head
for i in range(8):
  print curr._element
  curr = curr._next