Exemplo n.º 1
0
def mergeTwoLists(l1: LinkList, l2: LinkList) -> LinkList:
    if not l1:
        return l2

    if not l2:
        return l1

    if l1.value <= l2.value:
        l1.next = mergeTwoLists(l1.next, l2)
        return l1

    else:
        l2.next = mergeTwoLists(l1, l2.next)
        return l2
def reverseList(head: LinkList) -> LinkList:
    if head and head.next:
        first_pointer = head
        second_pointer = head.next

        first_pointer.next = None

        while second_pointer.next:
            third_pointer = second_pointer.next
            second_pointer.next = first_pointer
            first_pointer = second_pointer
            second_pointer = third_pointer

        second_pointer.next = first_pointer

        return second_pointer

    else:
        return head


ll = LinkList(20)
ll.next = LinkList(50)
ll.next.next = LinkList(10)
ll.next.next.next = LinkList(70)
ll.next.next.next.next = LinkList(90)

a = reverseList(ll)
ll.printLinkedList(a)
def compare(first_pointer, second_pointer):
    while second_pointer:
        if first_pointer.value != second_pointer.value:
            return False
        first_pointer = first_pointer.next
        second_pointer = second_pointer.next
    return True


def isPalindrome(head: LinkList) -> bool:
    if not head or not head.next:
        return True
    slow_pointer = head
    fast_pointer = head.next

    slow_pointer = get_mid(slow_pointer, fast_pointer)

    slow_pointer = reverse(slow_pointer)

    return compare(head, slow_pointer)


ll = LinkList(20)
ll.next = LinkList(50)
ll.next.next = LinkList(10)
ll.next.next.next = LinkList(50)
ll.next.next.next.next = LinkList(0)

a = isPalindrome(ll)
print(a)
Exemplo n.º 4
0
    if not l1:
        return l2

    if not l2:
        return l1

    if l1.value <= l2.value:
        l1.next = mergeTwoLists(l1.next, l2)
        return l1

    else:
        l2.next = mergeTwoLists(l1, l2.next)
        return l2



ll = LinkList(20)
ll.next = LinkList(50)
ll.next.next = LinkList(60)
ll.next.next.next = LinkList(70)
ll.next.next.next.next = LinkList(90)

ll1 = LinkList(30)
ll1.next = LinkList(40)
ll1.next.next = LinkList(55)
ll1.next.next.next = LinkList(80)
ll1.next.next.next.next = LinkList(100)


a = mergeTwoLists(ll, ll1)
ll.printLinkedList(a)