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
Пример #2
0
def findMidEle_1(list: LinkedList) -> int:
    length = list.length()
    mid_idx = length // 2
    temp = list.head
    idx = -1
    while (temp):
        idx = idx + 1
        if (idx == mid_idx):
            return temp.val
        temp = temp.next
Пример #3
0
def checkPalindro_2(list: LinkedList):
    if (not list):
        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]
Пример #4
0
def checkPalindro(list: LinkedList) -> bool:
    if (not list):
        return False
    if (list.length() <= 1):
        return True
    temp = list.head
    stack = []
    while (temp):
        stack.append(temp.val)
        temp = temp.next
    temp = list.head
    while (temp):
        ele = stack.pop()
        if (temp.val != ele):
            return False
        temp = temp.next
    return True