Example #1
0
        """

        if n <= 0 or not head:
            return head
        end = head

        for i in range(n):
            end = end.next

        current = head
        while end.next:
            current = current.next
            end = end.next

        current.next = current.next.next
        return head


lst = [7, 3, 10, 1, 4, 8, 11]
lnk_lst = build_linked_list(lst)

result = Solution().removeNthFromEnd(lnk_lst, 1)
assert (result.next.next.next.next.next.next is None)

result = Solution().removeNthFromEnd(lnk_lst, 3)
assert (result.next.next.next.next.value == 8)

assert (Solution().removeNthFromEnd(None, 1) == None)

assert (Solution().removeNthFromEnd(lnk_lst, -1) == lnk_lst)
Example #2
0
        if not head or not head.next:
            return head

        odd = head
        even = head.next
        even_head = head.next

        while even.next:
            odd_next = odd.next.next
            even_next = even.next.next

            odd.next = odd_next
            even.next = even_next

            odd = odd.next
            even = even.next

        odd.next = even_head

        return head


lst = [1, 2, 3, 4, 5, 6]
node = build_linked_list(lst)
res = Solution().oddEvenList(node)

assert (res.next.value == 3)
assert (res.next.next.value == 5)
assert (res.value == 1)
assert (res.next.next.next.value == 2)
Example #3
0
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        if not head:
            return
        dummy = node(-1)
        dummy.next = head
        fast = dummy
        slow = dummy

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        return slow


arr = [2, 5, 3, 4]
lst = build_linked_list(arr)
res = Solution().middleNode(lst)

print('a')
Example #4
0
        last = res

        while l1 or l2:
            ans = carry
            if l1:
                ans += l1.value
                l1 = l1.next
            if l2:
                ans += l2.value
                l2 = l2.next

            carry = ans // 10
            ans = ans % 10

            current = node(ans)
            last.next = current

            last = current

        if carry > 0:
            last.next = node(carry)
        return res.next


lst1 = build_linked_list([2, 4, 3])
lst2 = build_linked_list([5, 6, 4])

a = Solution().addTwoNumbers(lst1, lst2)

print("done")
Example #5
0
        """

        dummy = node(-1)
        current = dummy

        while l1 and l2:
            if l1.value < l2.value:
                current.next = l1
                l1 = l1.next
            else:
                current.next = l2
                l2 = l2.next
            current = current.next

        if l1:
            current.next = l1
        if l2:
            current.next = l2

        return dummy.next


arr1 = [2, 5, 8]
lst1 = build_linked_list(arr1)

arr2 = [1, 9, 10, 12]
lst2 = build_linked_list(arr2)

res = Solution().mergeTwoLists(lst1, lst2)

print('a')