コード例 #1
0
        return None

    while len(lists) > 1:
        new_lists = []
        for i in xrange(0, len(lists) - 1, 2):
            merged_list = merge(lists[i], lists[i + 1])
            new_lists.append(merged_list)
        if len(lists) % 2 == 1:
            new_lists.append(lists[-1])
        lists = new_lists

    return lists[0]


print "\n1. Reverse Test"
print_list(reverse(ListNode(1, ListNode(2, ListNode(3)))))
print "\n2. Reverse m-n test"
print_list(reverse_between(ListNode(1, ListNode(2, ListNode(3))), 1, 2))
print "\n3. Merge Test"
print_list(
    merge(ListNode(1, ListNode(3, ListNode(5))),
          ListNode(2, ListNode(4, ListNode(6)))))
print "\n4. Find Middle Test"
print find_middle(
    ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))).val
print "\n5. Add Node Test"
print_list(add_node(ListNode(1, ListNode(3)), ListNode(2, ListNode(5))))
print "\n6. Insert Node Test"
print_list(insert_node(ListNode(1, ListNode(3)), ListNode(2, ListNode(5)), 1))
print "\n7. Delete Node Test"
print_list(delete_node(ListNode(1, ListNode(2, ListNode(3))), 3))
コード例 #2
0
from ListNode import ListNode, print_list


class Solution:
    def reverse(self, head):
        prev, curt = None, head
        while curt is not None:
            temp = curt.next
            curt.next = prev
            prev = curt
            curt = temp
        return prev


sol = Solution()
node1 = ListNode(1, ListNode(2, ListNode(3)))
head = node1
print_list(head)
print_list(sol.reverse(head))
コード例 #3
0
ファイル: Rotate List.py プロジェクト: cnsapril/JZAlgo
    def rotateRight(self, head, k):
        if head is None:
            return None

        length = self.getLength(head)
        k = k % length

        dummy = ListNode(0, head)
        tail = dummy
        node = dummy

        for i in xrange(k):
            node = node.next

        while node.next is not None:
            node = node.next
            tail = tail.next

        node.next = dummy.next
        dummy.next = tail.next
        tail.next = None

        return dummy.next


sol = Solution()
print_list(sol.rotateRight(ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))), 3))


コード例 #4
0
from ListNode import ListNode, print_list, create_list

def remove_dups(head: ListNode) -> ListNode:
    """
    Write code to remove duplicates from an unsorted linked list.
    How would you solve this problem if a temporary buffer is not allowed?
    """

    p1 = head

    while p1:
        p2 = p1
        while p2.next:
            if p2.next.val == p1.val:
                p2.next = p2.next.next
            else:
                p2 = p2.next
        p1 = p1.next

    return head

print_list(remove_dups(create_list((3,3,6,4,4,5,6))))
コード例 #5
0
    dummy = ListNode(0)
    cur = dummy
    while l1 and l2:
        if l1.val < l2.val:
            cur.next = l1
            l1 = l1.next
        else:
            cur.next = l2
            l2 = l2.next
        cur = cur.next
    cur.next = l1 if l1 else l2
    return dummy.next

def getList():
    l1 = ListNode(1)
    l2 = ListNode(3)
    l3 = ListNode(6)
    l4 = ListNode(2)
    l5 = ListNode(7)
    l6 = ListNode(8)
    l1.next = l2
    l2.next = l3
    l4.next = l5
    l5.next = l6
    return l1, l4

if __name__ == "__main__":
    l1, l2 = getList()
    merged = mergeLists(l1, l2)
    print_list(merged)
コード例 #6
0
            head = head.next
        return head

    def reverse(self, head):
        prev, curt = None, head
        while curt is not None:
            temp = curt.next
            curt.next = prev
            prev = curt
            curt = temp
        return prev

    def reverse_between(self, head, m, n):
        dummy = ListNode(-1, head)
        pre_m = self.find_k_th(dummy, m - 1)
        m_th = pre_m.next
        n_th = self.find_k_th(dummy, n)
        post_n = n_th.next

        n_th.next = None
        self.reverse(m_th)

        pre_m.next = n_th
        m_th.next = post_n
        return dummy.next


sol = Solution()
head = ListNode(3760, ListNode(2881, ListNode(7595, ListNode(3904))))
print_list(sol.reverse_between(head, 1, 2))
コード例 #7
0
ファイル: Delete Duplicates.py プロジェクト: cnsapril/JZAlgo
class Solution:
    """
    @:param head: A ListNode
    @:return: A ListNode
    """
    def remove_duplicates(self, head):
        dummy = ListNode(0, head)
        prev, curt = dummy, dummy.next

        while curt is not None and curt.next is not None:
            if curt.val == curt.next.val:
                val = curt.val
                while curt is not None and curt.val == val:
                    curt = curt.next
                prev.next = curt
            else:
                prev = curt
                curt = curt.next

        return dummy.next


sol = Solution()
node3 = ListNode(3)
node2 = ListNode(2, node3)
node13 = ListNode(1, node2)
node12 = ListNode(1, node13)
node11 = ListNode(1, node12)
head = node11
print_list(sol.remove_duplicates(head))
コード例 #8
0
ファイル: Sort Linked List.py プロジェクト: cnsapril/JZAlgo
        tail = dummy
        while head1 is not None and head2 is not None:
            if head1.val < head2.val:
                tail.next = head1
                head1 = head1.next
            else:
                tail.next = head2
                head2 = head2.next
            tail = tail.next
        if head1 is not None:
            tail.next = head1
        else:
            tail.next = head2

        return dummy.next

    def sortList(self, head):
        if head is None or head.next is None:
            return head

        mid = self.findMiddle(head)
        right = self.sortList(mid.next)
        mid.next = None
        left = self.sortList(head)

        return self.merge(left, right)


sol = Solution()
print_list(sol.sortList(ListNode(3, ListNode(1, ListNode(5, ListNode(2))))))
コード例 #9
0
            tail.next = list2

        return dummy.next

    def mergeKLists(self, lists):
        if lists is None or not lists:
            return None

        while len(lists) > 1:
            new_lists = []
            for i in xrange(0, len(lists) - 1, 2):
                merged_list = self.merge(lists[i], lists[i + 1])
                new_lists.append(merged_list)
            if len(lists) % 2 == 1:
                new_lists.append(lists[-1])
            lists = new_lists

        return lists[0]


sol = Solution()
linked_lst = []
head1 = ListNode(1, ListNode(3, ListNode(5, ListNode(8))))
linked_lst.append(head1)
head2 = ListNode(0, ListNode(6, ListNode(10, ListNode(30))))
linked_lst.append(head2)
head3 = ListNode(4, ListNode(9, ListNode(23)))
linked_lst.append(head3)

print_list(sol.mergeKLists(linked_lst))
コード例 #10
0
        while head1 is not None and head2 is not None:
            if index % 2 == 0:
                dummy.next = head1
                head1 = head1.next
            else:
                dummy.next = head2
                head2 = head2.next
            index += 1
            dummy = dummy.next

        if head1 is not None:
            dummy.next = head1
        else:
            dummy.next = head2

    def reorderList(self, head):
        if head is None or head.next is None:
            return head

        mid = self.findMiddle(head)
        reversed_head = self.reverse(mid.next)
        mid.next = None

        self.merge(head, reversed_head)


sol = Solution()
head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
sol.reorderList(head)
print_list(head)
コード例 #11
0
from ListNode import ListNode, print_list


class Solution:
    def partition(self, head, x):
        left_head = ListNode(-1)
        right_head = ListNode(-1)
        left_tail, right_tail = left_head, right_head

        node = head
        while node is not None:
            if node.val < x:
                left_tail.next = node
                left_tail = left_tail.next
            else:
                right_tail.next = node
                right_tail = right_tail.next
            node = node.next

        right_tail.next = None
        left_tail.next = right_head.next

        return left_head.next


sol = Solution()
head = ListNode(
    1, ListNode(4, ListNode(3, ListNode(2, ListNode(5, ListNode(2))))))
print_list((sol.partition(head, 3)))