当前位置: 首页>>代码示例>>Python>>正文


Python list_node.ListNode类代码示例

本文整理汇总了Python中util.list_node.ListNode的典型用法代码示例。如果您正苦于以下问题:Python ListNode类的具体用法?Python ListNode怎么用?Python ListNode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ListNode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: partition1

 def partition1(self, head, x): #55ms, 45%
     """
     :type head: ListNode
     :type x: int
     :rtype: ListNode
     """
     if not head:
         return None
     dummy = ListNode(-1)
     dummy.next = head
     left = ListNode(-1)
     ltail = left
     right = ListNode(-1)
     rtail = right
     tmp = head
     while tmp:
         if tmp.val<x:
             ltail.next = tmp
             ltail = ltail.next
         else:
             rtail.next = tmp
             rtail = rtail.next
         tmp = tmp.next
     if left.next:  # need to check if left part is None, otherwise wrong for case2
         dummy.next = left.next
         ltail.next = right.next
     else:
         dummy.next = right.next
     rtail.next = None
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:30,代码来源:partition_list.py

示例2: rotateRight

    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head

        cur = head
        L = 1
        while cur.next is not None:
            cur = cur.next
            L += 1

        cur.next = head  # previous is NULL

        size = L - k % L
        dummy = ListNode(-1)
        dummy.next = head

        for i in range(size):
            dummy = dummy.next

        ret = dummy.next
        dummy.next = None
        return ret
开发者ID:harrifeng,项目名称:leet-in-python,代码行数:27,代码来源:061_rotate_list.py

示例3: test_case2

 def test_case2(self):
     head = ListNode(0)
     head.next = None
     n = 1
     answer = None
     result = self.sol.removeNthFromEnd(head, n)
     self.assertEqual(answer, result)
开发者ID:linglingithub,项目名称:testhello,代码行数:7,代码来源:remove_nth_node_from_end_of_list.py

示例4: deleteDuplicates

 def deleteDuplicates(self, head): #58ms, 65%
     """
     :type head: ListNode
     :rtype: ListNode
     """
     if not head:
         return None
     dummy = ListNode(0)
     dummy.next = head
     good = dummy
     first = head
     faster = head.next
     while first:
         if faster is None or first.val != faster.val:
             good.next = first
             good = good.next
             first = faster
             if faster:
                 faster = faster.next
         else:
             while faster and faster.val == first.val:
                 faster = faster.next
             first = faster
             faster = first.next if first else None
     good.next = None # need to add ending to good, otherwise wrong for case 2
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:26,代码来源:remove_duplicates_from_sorted_listII.py

示例5: deleteDuplicates

    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head

        dummy = ListNode(-1)
        dummy.next = head
        ###############################################################
        # like remove duplicate from array, pre is the last valid one #
        # and pre.next == cur means the left-side OK one              #
        ###############################################################
        pre = dummy
        cur = head

        while cur.next is not None:
            if cur.val != cur.next.val:  # right-side ok
                if pre.next == cur:      # if left-side ok, add one to result
                    pre = pre.next
                else:
                    pre.next = cur.next  # left-side not ok, new start
            cur = cur.next

        if pre.next == cur:     # cur is the last one, add it to result
            pre = pre.next
        pre.next = None
        return dummy.next
开发者ID:harrifeng,项目名称:leet-in-python,代码行数:29,代码来源:082_remove_duplicates_from_sorted_list_ii.py

示例6: swapPairs

    def swapPairs(self, head): #45ms, 83%
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head
        dummy = ListNode(0)
        dummy.next = head
        zero = dummy
        one = head
        two = head.next
        while one and two:
            three = two.next
            # one.val, two.val = two.val, one.val   # 32ms, 98%, if use the swap value way
            # zero = two
            # one = three
            # two = one.next if one else None
            zero.next = two
            one.next = three
            two.next = one
            zero = one
            one = one.next
            two = one.next if one else None

        return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:26,代码来源:swap_nodes_in_pairs.py

示例7: test_case1

 def test_case1(self):
     nums = [1,2,3,4,5]
     m =2
     n = 4
     answer = [1,4,3,2,5]
     result = self.sol.reverseBetween(ListNode.parseArray2List(nums),m, n)
     self.assertEqual(answer, ListNode.parseList2Array(result))
开发者ID:linglingithub,项目名称:testhello,代码行数:7,代码来源:reverse_linked_listII.py

示例8: reverseKGroup

 def reverseKGroup(self, head, k): # 85ms, 37%, pay attention to link details, trial and error process
     """
     :type head: ListNode
     :type k: int
     :rtype: ListNode
     """
     if not head:
         return None
     if k==1:
         return head
     dummy = ListNode(-1)
     dummy.next = head
     p = dummy
     done_tail = dummy
     cnt = 0
     while True:
         while cnt < k:
             p = p.next
             if p:
                 cnt+=1
             else:
                 break
         if cnt < k or not p:
             return dummy.next
         next_head = p.next
         new_done = done_tail.next
         done_tail.next = self.reverse_link(done_tail.next,next_head)
         done_tail = new_done
         p = next_head
         if not p:
             return dummy.next
         cnt = 1
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:33,代码来源:reverse_nodes_in_k_group.py

示例9: test_case4

 def test_case4(self):
     head = ListNode(0)
     n1 = ListNode(1)
     head.next = n1
     n = 1
     answer = head
     result = self.sol.removeNthFromEnd(head, n)
     self.assertEqual(None, result.next)
开发者ID:linglingithub,项目名称:testhello,代码行数:8,代码来源:remove_nth_node_from_end_of_list.py

示例10: removeNthFromEnd2

 def removeNthFromEnd2(self, head, n): # slower 72ms
     dummy=ListNode(0); dummy.next=head
     p1=p2=dummy
     for i in range(n): p1=p1.next
     while p1.next:
         p1=p1.next; p2=p2.next
     p2.next=p2.next.next
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:8,代码来源:remove_nth_node_from_end_of_list.py

示例11: test_case3

 def test_case3(self):
     head = ListNode(0)
     n1 = ListNode(1)
     head.next = n1
     n = 2
     answer = n1
     result = self.sol.removeNthFromEnd(head, n)
     self.assertEqual(answer, result)
开发者ID:linglingithub,项目名称:testhello,代码行数:8,代码来源:remove_nth_node_from_end_of_list.py

示例12: reverse_link

 def reverse_link(self, head, tail):
     dummy = ListNode(-1)
     dummy.next = head
     p = head.next
     while p!=tail:
         new_next = p.next
         head.next = new_next
         p.next = dummy.next
         dummy.next = p
         p = new_next
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:11,代码来源:reverse_nodes_in_k_group.py

示例13: reverseBetween_ref

 def reverseBetween_ref(self, head, m, n):
     if head == None or head.next == None:
         return head
     dummy = ListNode(0); dummy.next = head
     head1 = dummy
     for i in range(m - 1):
         head1 = head1.next
     p = head1.next
     for i in range(n - m):
         tmp = head1.next
         head1.next = p.next
         p.next = p.next.next
         head1.next.next = tmp
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:14,代码来源:reverse_linked_listII.py

示例14: deleteDuplicates_ref

 def deleteDuplicates_ref(self, head): #69ms, 35%
     if head == None or head.next == None:
         return head
     dummy = ListNode(0); dummy.next = head
     p = dummy
     tmp = dummy.next
     while p.next:
         while tmp.next and tmp.next.val == p.next.val:
             tmp = tmp.next
         if tmp == p.next:
             p = p.next
             tmp = p.next
         else:
             p.next = tmp.next
     return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:15,代码来源:remove_duplicates_from_sorted_listII.py

示例15: reverseList_itera

    def reverseList_itera(self, head): # iterative way , 49ms, 67%
        """
        remember to do it in on pass!!!
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None:
            return head
        dummy = ListNode(0)

        while head:
            tmp = head
            head = head.next
            tmp.next = dummy.next
            dummy.next = tmp
        return dummy.next
开发者ID:linglingithub,项目名称:testhello,代码行数:16,代码来源:reverse_linked_list.py


注:本文中的util.list_node.ListNode类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。