|
| 1 | +#!/usr/bin/python3 |
| 2 | +import unittest |
| 3 | +from random import randint |
| 4 | +''' |
| 5 | +Delete Middle Node: 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. EXAMPLE lnput:the node c from the linked lista->b->c->d->e->f Result: nothing is returned, but the new linked list looks like a->b->d->e->f Hints:#72 |
| 6 | +''' |
| 7 | + |
| 8 | +''' |
| 9 | +Get clarification on this question: |
| 10 | +1. Will I always know that I will get a middle node? (non end or head node) |
| 11 | +2. What if the node is only 2 lengths long? |
| 12 | +3. What input will I get? |
| 13 | +''' |
| 14 | + |
| 15 | + |
| 16 | +class Node: |
| 17 | + def __init__(self, data, next=None): |
| 18 | + self.data = data |
| 19 | + self.next = next |
| 20 | + |
| 21 | + def __str__(self): |
| 22 | + return str(self.data) |
| 23 | + |
| 24 | + |
| 25 | +class SinglyLinkedList: |
| 26 | + def __init__(self): |
| 27 | + self.head = None |
| 28 | + |
| 29 | + def addNode(self, data): |
| 30 | + # adds node to head if no head |
| 31 | + node = Node(data) |
| 32 | + if not self.head: |
| 33 | + self.head = node |
| 34 | + # adds to end of linked list |
| 35 | + else: |
| 36 | + temp = self.head |
| 37 | + while temp.next: |
| 38 | + temp = temp.next |
| 39 | + temp.next = node |
| 40 | + |
| 41 | + def printList(self): |
| 42 | + if self.head is None: |
| 43 | + print('Nothing to print') |
| 44 | + return |
| 45 | + |
| 46 | + temp = self.head |
| 47 | + while temp: |
| 48 | + print(temp.data) |
| 49 | + temp = temp.next |
| 50 | + |
| 51 | + def findSize(self): |
| 52 | + if self.head is None: |
| 53 | + print('Nothing to find the size of') |
| 54 | + return |
| 55 | + |
| 56 | + temp = self.head |
| 57 | + count = 0 |
| 58 | + while temp: |
| 59 | + count += 1 |
| 60 | + temp = temp.next |
| 61 | + return count |
| 62 | + |
| 63 | + def findMiddleNode(self): |
| 64 | + size = self.findSize() |
| 65 | + if size and size > 2: |
| 66 | + # cannot be the size of the node (tail) or the first node (head) |
| 67 | + temp = self.head |
| 68 | + randomNum = randint(2, size - 1) |
| 69 | + for x in range(1, randomNum): |
| 70 | + temp = temp.next |
| 71 | + return temp |
| 72 | + else: |
| 73 | + print("Not able to access a non head or tail node") |
| 74 | + return None |
| 75 | + |
| 76 | + def deleteMiddleNode(self, node): |
| 77 | + '''The solution is simply to copy the data |
| 78 | + from the next node over to the current node, |
| 79 | + and then to delete the next node. |
| 80 | + ''' |
| 81 | + if node is None: |
| 82 | + print('No node to delete') |
| 83 | + return |
| 84 | + |
| 85 | + temp = node.next |
| 86 | + node.data = temp.data |
| 87 | + node.next = temp.next |
| 88 | + del temp |
| 89 | + |
| 90 | + |
| 91 | +class Test(unittest.TestCase): |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def setUpClass(cls): |
| 95 | + cls.l = SinglyLinkedList() |
| 96 | + cls.l.addNode(-1) |
| 97 | + cls.l.addNode(3) |
| 98 | + cls.l.addNode('hello') |
| 99 | + cls.l.addNode(100) |
| 100 | + cls.l.addNode(5) |
| 101 | + cls.l.printList() |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def tearDownClass(self): |
| 105 | + pass |
| 106 | + |
| 107 | + def test1(self): |
| 108 | + numNodes = self.l.findSize() |
| 109 | + print('Number of nodes {}'.format(numNodes)) |
| 110 | + |
| 111 | + def test2(self): |
| 112 | + node = self.l.findMiddleNode() |
| 113 | + print('Middle node -->', node.data) |
| 114 | + print('Deleting middle node') |
| 115 | + self.l.deleteMiddleNode(node) |
| 116 | + self.l.printList() |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + unittest.main() |
0 commit comments