Ejemplo n.º 1
0
# Implement an algorithm to delete a node in the middle (i.e., any node but
# the first and last node, not necessarily the exact middle) of a singly linked list, given only access to
# that node.

from Class_Linkedlist import randomLinkedList


def deleteNode(linkedlist, node_value):
    curr = linkedlist.head
    while curr.next is not None:
        if curr.next.value == node_value:
            curr.next = curr.next.next
            return
        else:
            curr = curr.next
    print('{} value is not in list'.format(node_value))


L1 = randomLinkedList(9, 1, 8)
print(L1)
deleteNode(L1, 5)
print(L1)
Ejemplo n.º 2
0
        while runn.next is not None:
            if runn.next.value == curr.value:
                runn.next = runn.next.next
            else:
                runn = runn.next
        curr = curr.next


# Alternate solution

def delDuplicates2(linkedlist):     #O(n) time, O(n) space solution
    curval = linkedlist.head
    tempdict = {curval.value : True}
    while curval.next is not None:
        if curval.next.value in tempdict:
            curval.next = curval.next.next
        else:
            tempdict[curval.next.value] = True
            curval = curval.next


L1 = randomLinkedList(9, 3, 7)  #Length, min ,max
print(L1)
delDuplicates(L1)
print(L1)

L2 = randomLinkedList(9, 3, 7)
print(L2)
delDuplicates2(L2)
print(L2)
Ejemplo n.º 3
0
    result = []
    while head1 is not None or head2 is not None:
        h1val = 0 if head1 is None else head1.value
        h2val = 0 if head2 is None else head2.value
        temp = h1val + h2val + carry
        carry = 0
        if temp > 9:
            result.append(int(temp % 10))
            carry = int(temp / 10)
        else:
            result.append(temp)
        if head1:
            head1 = head1.next
        if head2:
            head2 = head2.next
    if carry:
        result.append(carry)
    return result


L1 = randomLinkedList(5, 1, 9)
print(L1)
L2 = randomLinkedList(2, 1, 9)
print(L2)
results = add_linkedlist(L1, L2)
#print(results)
res = LinkedList()

for i in results[::-1]:
    res.makeNode(i)
res.printlist()
Ejemplo n.º 4
0
Archivo: 2.py Proyecto: raj-shah14/Ctci
    while ptr1 is not None:
        ptr1 = ptr1.next
        ptr2 = ptr2.next
    return ptr2.value  # O(n) time and O(1) space.

    #Here i am not counting the length of my Linked list.


# Deleting Kth from Last
def deleteKthfromlast(linkedlist, index):
    lenll = getLength(linkedlist)
    if index == 0:
        return "Give index greater than 0"
    if index > lenll:
        return '{}th index is out of bound'.format(index)
    elem = lenll - index
    curr = linkedlist.head
    while elem > 1:
        curr = curr.next
        elem -= 1
    curr.next = curr.next.next


L1 = randomLinkedList(9, 2, 9)
print(L1)
print("My approach: " + str(getKthfromlast(L1, 6)))
print("Iterative 2 pointer appraoch: " + str(iterativeKthElement(L1, 6)))
getKthWithRecursion(L1.head, 6)
# deleteKthfromlast(L1,6)
# print(L1)