Esempio n. 1
0
    def test_peek_last(self):
        llist = SinglyLinkedList()
        self.assertRaises(Exception, llist.peek_last)

        llist.insert_first(0)
        self.assertEqual(llist.peek_last(), 0)
        llist.insert_first(-1)
        self.assertEqual(llist.peek_last(), 0)
Esempio n. 2
0
    def test_clear(self):
        llist = SinglyLinkedList()
        llist.insert_first(0)
        self.assertEqual(llist.size, 1)
        self.assertEqual(llist.head.data, 0)

        llist.clear()
        self.assertEqual(llist.size, 0)
        self.assertEqual(llist.head, None)
Esempio n. 3
0
    def test_insert_first(self):
        llist = SinglyLinkedList()
        llist.insert_first(1)
        self.assertEqual(llist.size, 1)
        self.assertEqual(llist.head.data, 1)

        llist.insert_first(0)
        self.assertEqual(llist.size, 2)
        self.assertEqual(llist.head.data, 0)
        self.assertEqual(llist.head.next.data, 1)
Esempio n. 4
0
 def test_is_empty(self):
     llist = SinglyLinkedList()
     self.assertTrue(llist.is_empty())
     llist.insert_first(0)
     self.assertFalse(llist.is_empty())
Esempio n. 5
0
 def test_contains(self):
     llist = SinglyLinkedList()
     llist.insert_first(0)
     self.assertTrue(llist.contains(0))
     self.assertFalse(llist.contains(1))
Esempio n. 6
0
from singly_linked_list import SinglyLinkedList


def kth_to_last(k, singly_linked_list: SinglyLinkedList):
    kth = singly_linked_list._head
    end = singly_linked_list._head
    counter = 0
    while end is not None:
        if counter >= k:
            kth = kth._next
        end = end._next
        counter += 1
    return kth


l = SinglyLinkedList()
l.insert_first(1)
l.insert_first(2)
l.insert_first(3)
l.insert_first(1)
l.insert_first(3)
l.insert_first(4)
l.insert_first(1)
l.insert_first(1)
print(l)
print(kth_to_last(6, l))

print(l)
i = 1