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


Python ListNode.ListNode类代码示例

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


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

示例1: deleteDuplicates

    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        
        if not head:
            return head
        
        # a dummy node is must
        dummy = ListNode(0)
        dummy.next = head
        
        prev = dummy
        current = head
        
        while current:
            
            if current.next and current.val == current.next.val:

                # find duplciate, delete all
                while current.next and current.val == current.next.val:
                    current = current.next
                
                # now current pointer is at last node of duplicated
                # delete duplicated nodes
                prev.next = current.next
                current = current.next
                # @note: prev stays the same
                
            else:
                prev = current
                current = current.next
        
        return dummy.next # return could be a empty list, if input list with all duplicates
开发者ID:keypointt,项目名称:leetcode,代码行数:35,代码来源:Remove_Duplicates_from_Sorted_List_II.py

示例2: generate_test_list2

def generate_test_list2():
    n1 = ListNode(1)
    n2 = ListNode(2)

    n1.next = n2

    return n1
开发者ID:victorfei,项目名称:interview,代码行数:7,代码来源:rotateList.py

示例3: rotateLSegment

def rotateLSegment(l, m, n):
    dummyN = ListNode(0)
    dummyN.next = l

    l = dummyN
    rslt, i = l, 0

    while i < m-1:
        l = l.next
        i += 1

    t, i = None, 0

    beg, end = l, l.next
    l = l.next

    while l and i <= n-m:
        h = l
        l = l.next
        h.next = t
        t = h
        i += 1

    beg.next = t
    end.next = l

    return dummyN.next
开发者ID:victorfei,项目名称:interview,代码行数:27,代码来源:rotateListSegment.py

示例4: test3

def test3():
    n1 = ListNode(1)
    n2 = ListNode(2)

    n1.next = n2

    ListNode.print_ll(reorderList(n1))
开发者ID:victorfei,项目名称:interview,代码行数:7,代码来源:reorderList.py

示例5: revKGroup_r

def revKGroup_r(h, k):
    count = 0
    dummy = ListNode(-1)
    dummy.next = h
    n = dummy
    head = dummy

    while head:
        tail = head.next
        n = tail

        while n and count < k:
            n = n.next
            count += 1

        if count < k and not n:
            break

        count = 0
        stopN = n

        head.next = revK_r(tail, stopN)
        tail.next = stopN
        head = tail

    return dummy.next
开发者ID:victorfei,项目名称:interview,代码行数:26,代码来源:reverseInKGrp.py

示例6: oddEvenList

 def oddEvenList(self, head):
     """
     :type head: ListNode
     :rtype: ListNode
     """
     if not head:
         return
     dummy1, dummy2 = ListNode(0), ListNode(0)
     dummy1.next = head
     dummy2.next = head.next
     dummy3 = head.next
     count = 3
     while head and dummy3 and dummy3.next and head.next:
         if head.next.next and not dummy3.next.next:
             head.next = head.next.next
             head = head.next
             break
         elif not head.next.next and dummy3.next.next:
             dummy3.next = dummy3.next.next
             dummy3 = dummy3.next
             break
         if count%2:
             head.next = head.next.next
             head = head.next
         else:
             dummy3.next = dummy3.next.next
             dummy3 = dummy3.next
     head.next = dummy2.next
     if dummy3:
         dummy3.next = None
     return dummy1.next
开发者ID:zymtech,项目名称:leetcode,代码行数:31,代码来源:odd-even-linked-list.py

示例7: insertAtHead

    def insertAtHead(self, item):
        
        '''
            pre:  an item to be inserted into list
            post: item is inserted into beginning of list
        '''

        node = ListNode(item)

        if not self.length:
            # set the cursor to the head because it's the
            # first item inserted into the list
            self.head = node 
        elif self.length == 1 and not self.tail:
            self.tail = self.head
            node.link = self.tail
            self.head = node
        else:
            node.link = self.head
            self.head = node

        # set the cursor to the head if it's not set yet
        if not self.cursor:
            self.cursor = self.head

        self.length += 1
开发者ID:NickHurst,项目名称:Linked-List,代码行数:26,代码来源:LinkedListCursor.py

示例8: reverseLinkedListII

def reverseLinkedListII(head, m, n):
    if head == None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    head = dummy

    # find premmNode and mNode
    for i in range(1, m):
        head = head.next
    prevmNode = head
    mNode = head.next

    # reverse link from m to n
    nNode = mNode
    nextnNode = nNode.next
    for i in range(m, n):
        temp = nextnNode.next
        nextnNode.next = nNode
        nNode = nextnNode
        nextnNode = temp

    # combine
    prevmNode.next = nNode
    mNode.next = nextnNode

    return dummy.next
开发者ID:cutewindy,项目名称:CodingInterview,代码行数:27,代码来源:reverseLinkedListII.py

示例9: generate_test_list3

def generate_test_list3():
    n1 = ListNode(1)
    n2 = ListNode(2)
    n3 = ListNode(3)

    n1.next = n2
    n2.next = n3

    return n1
开发者ID:victorfei,项目名称:interview,代码行数:9,代码来源:rotateList.py

示例10: addNumbers

def addNumbers(h1, h2):
  l1, l2 = getLeng(h1), getLeng(h2)
  needToAdd1 = addNumRecur(h1, h2, l1 >= l2, abs(l1-l2))
  if needToAdd1:
    newN = ListNode(1)
    newN.next = (h1 if l1>=l2 else h2)
    return newN
  else:
    return (h1 if l1>=l2 else h2)
开发者ID:jfriend08,项目名称:practice,代码行数:9,代码来源:addNumbers2.py

示例11: test3

def test3():
    n1 = ListNode(1)
    n2 = ListNode(2)

    n2.next = n1

    h = n2
    rslt = mergeSort(h)
    ListNode.print_ll(rslt)
开发者ID:victorfei,项目名称:interview,代码行数:9,代码来源:sortList.py

示例12: test2

def test2():
    n1a = ListNode(1)
    n1b = ListNode(1)
    n1c = ListNode(1)

    n1a.next = n1b
    n1b.next = n1c
    rslt = removeElement(n1a, 1)
    print(ListNode.print_ll(rslt))
开发者ID:victorfei,项目名称:interview,代码行数:9,代码来源:removeElement.py

示例13: test_quicksort_list

 def test_quicksort_list(self):
     A = ListNode.makeList([5, 4, 3, 2, 1, 6, 10, 7, 8, 9])
     quicksort_list(A, None)
     A.show()
     A = ListNode.makeList([2, 1, 2, 4, 5, 9, 8])
     quicksort_list(A, None)
     A.show()
     A = ListNode.makeList([10])
     quicksort_list(A, None)
     A.show()
开发者ID:pengmeng,项目名称:LeetCode,代码行数:10,代码来源:test_quicksort.py

示例14: removeElement

def removeElement(n, val):
    dummyN = ListNode(-1)
    dummyN.next = n
    h = dummyN
    while h:
        if h.next and h.next.val == val:
            h.next = h.next.next
        else:
            h = h.next

    return dummyN.next
开发者ID:victorfei,项目名称:interview,代码行数:11,代码来源:removeElement.py

示例15: test004

 def test004(self):
     
     print "test 004"
     
     n0 = ListNode(0)
     n0.next = n0
     
     hasCycle = Solution().hasCycle(n0)
     
     print "  expect:\t", True
     print "  output:\t", hasCycle
     print
开发者ID:54lihaoxin,项目名称:leetcode_python,代码行数:12,代码来源:test_suite.py


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