def test_length(self): ll = LinkedList() assert ll.length() == 0 ll.append('A') assert ll.length() == 1 ll.append('B') assert ll.length() == 2 ll.append('C') assert ll.length() == 3
def test_append(self): ll = LinkedList() ll.append('A') assert ll.head.data == 'A' assert ll.tail.data == 'A' assert ll.as_list() == ['A'] ll.append('B') assert ll.head.data == 'A' assert ll.tail.data == 'B' assert ll.as_list() == ['A', 'B'] ll.append('C') assert ll.head.data == 'A' assert ll.tail.data == 'C' assert ll.as_list() == ['A', 'B', 'C']
return False if (list.length() <= 1): return True temp = list.head re = '' while (temp): re = re + temp.val temp = temp.next return re == re[::-1] if __name__ == '__main__': # Start with the empty list llist = LinkedList() # Insert a. So linked list becomes 6->None llist.append('a') # Insert b at the beginning. So linked list becomes b->a->None llist.push('b') # Insert b at the beginning. So linked list becomes b->a->b->None llist.append('b') # print list llist.printList() print(checkPalindro(llist)) print('-------------') print(checkPalindro_2(llist))
llist = LinkedList() # test remove duplicates in a sorted list llist.push(20) llist.push(13) llist.push(13) llist.push(11) llist.push(11) llist.push(11) print("Created Linked List: ") llist.printList() print() print("Linked List after removing", "duplicate elements:") llist = removeDup(llist) llist.printList() # test remove duplicates in an unsorted list list = LinkedList() list.append(10) list.append(12) list.append(11) list.append(11) list.append(12) list.append(11) list.append(10) print("Created Linked List: ") list.printList() print("Linked List after removing", "duplicate elements:") list = removeDuplicates(list) list.printList()
third = Node(3) llist.head.next = second; second.next = third; # intial Linked List llist.printList() # Inserting node at the front llist.push(0) # updating the Linked List print("new updated list after adding element at the stating of the list") llist.printList() # Inserting element after specific element llist.insertAfterData(2, 4) print("updated linked list after insertion after the given node") llist.printList() llist.insertAfterNode(second, 5) print("updated linked list after insertion after the given node") llist.printList() llist.append(6) print("updated list after appending data") llist.printList() llist.delete(9) print("updated list after deleting an element") llist.printList()
percent = percent length = int(howmuchichoose(LL.size(), percent)) # print(length) LL.driff(length) # print("Now : ",end='') print(LL) def deBottomUp(percent, LL): # print("De-BottomUp Activated!!!") percent = percent length = int(howmuchichoose(LL.size(), percent)) # print(length) for i in range(0,length): LL.tailtohead() # print("Now : ",end='') print(LL) LL = LinkedList() for i in range(1,11): LL.append(str(i)) # print("start ",end='') # print(LL) bottomUp(130,LL) riffle(60,LL) deRiffle(60,LL) deBottomUp(130,LL)
slow, fast = temp, temp while (slow and fast and fast.next): fast_next = fast.next fast = fast_next.next slow = slow.next return slow.val if __name__ == '__main__': # Start with the empty list llist = LinkedList() # Insert 6. So linked list becomes 6->None llist.append(6) # Insert 7 at the beginning. So linked list becomes 7->6->None llist.push(7) # Insert 1 at the beginning. So linked list becomes 1->7->6->None llist.push(1) # Insert 4 at the end. So linked list becomes 1->7->6->4->None llist.append(4) # Insert 10 at the end. So linked list becomes 1->7->6->4->10->None llist.append(10) # Insert 8, after 7. So linked list becomes 1 -> 7-> 8-> 6-> 4-> 10 ->None llist.insertAfter(llist.head.next, 8)