本文整理汇总了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
示例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
示例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)
示例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
示例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
示例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
示例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))
示例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
示例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)
示例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
示例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)
示例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
示例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
示例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
示例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