from typing import List
from util import ListNode, lc_list2singlelinkedlist, lc_singlelinkedlist2list


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        rtn = head
        while head:
            if head.next and head.val == head.next.val:
                nxt = head.next
                head.next = head.next.next
                del nxt
            else:
                head = head.next
        return rtn


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [1, 1, 2, 3, 3],
        [1, 1, 1, 2, 2, 3],
    ]
    for i in test_cases:
        i = lc_list2singlelinkedlist(i)
        result = sol.deleteDuplicates(i)
        print(lc_singlelinkedlist2list(result))
Ejemplo n.º 2
0

class Solution:
    def insertionSortList(self,
                          head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(-5001)
        dummy.next = to_insert = head
        while head and head.next:
            if head.val > head.next.val:
                to_insert = head.next
                pre = dummy
                while pre.next.val < to_insert.val:
                    pre = pre.next
                head.next = to_insert.next
                to_insert.next = pre.next
                pre.next = to_insert
            else:
                head = head.next
        return dummy.next


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [4, 2, 1, 3],
        [-1, 5, 3, 4, 0],
    ]
    for i in test_cases:
        dummyult = sol.insertionSortList(lc_list2singlelinkedlist(i))
        print(lc_singlelinkedlist2list(dummyult))
Ejemplo n.º 3
0
        if not head:
            return None
        tail = head
        n = 1
        while tail.next:
            tail = tail.next
            n += 1
        mod = k % n
        if mod == 0:
            return head
        pt = head
        for i in range(n - mod - 1):
            pt = pt.next
        rtn = pt.next
        pt.next = None
        tail.next = head
        return rtn


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [[1, 2, 3, 4, 5], 2],
        [[1, 2, 3, 4, 5, 6], 2],
        [[1, 2, 3, 4, 5, 6], 12],
        [[1, 2, 3], 4],
    ]
    for i, j in test_cases:
        result = (sol.rotateRight(lc_list2singlelinkedlist(i), j))
        print(lc_singlelinkedlist2list(result))
            return None
        sp = ListNode()
        sp.next = head
        pt = head
        pre = sp
        while pt and pt.next:
            if pt.val == pt.next.val:
                while pt.next and pt.val == pt.next.val:
                    pt.next = pt.next.next
                pre.next = pt.next
                pt = pre.next
            else:
                pre = pt
                pt = pt.next
        return sp.next


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [],
        [1, 2, 3, 3, 4, 4, 5],
        [0, 0, 1, 1, 1, 1, 2, 3, 3],
        [1, 1, 1, 2, 3],
    ]
    for i in test_cases:
        result = sol.deleteDuplicates(lc_list2singlelinkedlist(i))
        print(lc_singlelinkedlist2list(result))


Ejemplo n.º 5
0
            start = head
            head = head.next
            cnt += 1

        pre = None
        while head and cnt != right:
            nxt = head.next
            head.next = pre
            pre = head
            head = nxt
            cnt += 1

        if head:
            start.next.next = head.next
            head.next = pre
            start.next = head

        return rtn.next


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [[3, 5], 1, 1],
        [[1, 2, 3, 4, 5], 2, 4],
        [[5], 1, 1],
    ]
    for i, j, k in test_cases:
        result = sol.reverseBetween(lc_list2singlelinkedlist(i), j, k)
        print(lc_singlelinkedlist2list(result))
Ejemplo n.º 6
0
class Solution:
    def sortedListToBST(self, head: ListNode) -> TreeNode:
        if not head:
            return None
        slow, fast = head, head
        pre = None
        while fast and fast.next:
            pre = slow
            slow = slow.next
            fast = fast.next.next
        if pre:
            pre.next = None
        rtn = TreeNode(slow.val)
        rtn.left = self.sortedListToBST(head) if head != slow else None
        rtn.right = self.sortedListToBST(slow.next)
        return rtn


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [-10, -3, 0, 5, 9, 12],
        [-10, -3, 0, 5, 9],
    ]
    for i in test_cases:
        result = sol.sortedListToBST(lc_list2singlelinkedlist(i))
        print(lc_tree2list(result))


Ejemplo n.º 7
0
        pt = head
        pre = dict()
        cnt = 1
        while pt.next:
            pre[pt.next] = pt
            pt = pt.next
            cnt += 1
        # now pt is the last node
        # print(cnt)
        pt2 = head
        cnt = (cnt + 1) // 2
        while cnt > 1:
            pt.next = pt2.next
            pt2.next = pt
            pt2 = pt.next
            pt = pre[pt]
            cnt -= 1
        pt.next = None
        print(lc_singlelinkedlist2list(head))


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [1],
        [1, 2, 3, 4],
        [1, 2, 3, 4, 5]
    ]
    for i in test_cases:
        sol.reorderList(lc_list2singlelinkedlist(i))
class Solution:
    def getIntersectionNode(self, headA: ListNode,
                            headB: ListNode) -> ListNode:
        a = set()
        while headA:
            a.add(headA)
            headA = headA.next
        while headB:
            if headB in a:
                return headB
            headB = headB.next
        return None


if __name__ == "__main__":
    sol = Solution()
    headA = [4, 1, 8, 4, 5]
    headB = [5, 6, 1, 8, 4, 5]
    headA = lc_list2singlelinkedlist(headA)
    headB = lc_list2singlelinkedlist(headB)
    tmpA = headA
    while tmpA.val != 8:
        tmpA = tmpA.next

    tmpB = headB
    while tmpB.val != 1:
        tmpB = tmpB.next
    tmpB.next = tmpA
    print(sol.getIntersectionNode(headA, headB))
Ejemplo n.º 9
0
class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        s_pt = s_cur = ListNode()
        l_pt = l_cur = ListNode()
        while head:
            tmp = head.next
            if head.val < x:
                s_cur.next = head
                s_cur = s_cur.next
            else:
                l_cur.next = head
                l_cur = l_cur.next
            head.next = None
            head = tmp
        s_cur.next = l_pt.next
        return s_pt.next


if __name__ == "__main__":
    sol = Solution()
    test_cases = [
        [[1, 4, 3, 2, 5, 2], 3],
        [[1, 4, 3, 2, 5, 2], 4],
    ]
    for i, j in test_cases:
        result = sol.partition(lc_list2singlelinkedlist(i), j)
        print(lc_singlelinkedlist2list(result))


Ejemplo n.º 10
0
import bisect
import math
from typing import List
from collections import deque, defaultdict, OrderedDict
from util import (
    TreeNode, lc_list2tree,
    ListNode, lc_list2singlelinkedlist
)


class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        l = []
        while head:
            l.append(head)
            head = head.next
        return l[len(l) // 2]


if __name__ == "__main__":
    sol = Solution()
    l = [1,2,3,4,5,6]
    l = [1,2,3,4,5]
    l = lc_list2singlelinkedlist(l)
    print(sol.middleNode(l))