本文整理汇总了Python中ListNode.ListNode.val方法的典型用法代码示例。如果您正苦于以下问题:Python ListNode.val方法的具体用法?Python ListNode.val怎么用?Python ListNode.val使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ListNode.ListNode
的用法示例。
在下文中一共展示了ListNode.val方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: swapNodes
# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import val [as 别名]
def swapNodes(self, head):
stack = []
slowptr = head
fastptr = head
temp = head
while fastptr.next != None and fastptr.next.next != None:
fastptr = fastptr.next.next
slowptr = slowptr.next
slowptr_copy = slowptr.next
slowptr.next = None
while slowptr_copy != None:
stack.append(slowptr_copy.val)
slowptr_copy = slowptr_copy.next
while len(stack) != 0:
newNode = ListNode(float("-inf"))
newNode.val = stack.pop()
newNode.next = temp.next
temp.next = newNode
temp = temp.next.next
return head
示例2: ListNode
# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import val [as 别名]
__author__ = 'Lei Chen'
from ListNode import ListNode
n1 = ListNode(1)
n2 = ListNode(2)
n3 = ListNode(3)
n4 = ListNode(4)
n5 = ListNode(5)
n1.val = 1
n2.val = 2
n3.val = 3
n4.val = 4
n5.val = 5
n1.next = n2
# n2.next = n3
# n3.next = n4
# n4.next = n5
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @return {ListNode}
def reverseList(self, head):
示例3: ListNode
# 需要导入模块: from ListNode import ListNode [as 别名]
# 或者: from ListNode.ListNode import val [as 别名]
__author__ = 'Lei Chen'
from ListNode import ListNode
n1 = ListNode(1)
n2 = ListNode(2)
n3 = ListNode(3)
n4 = ListNode(4)
n5 = ListNode(5)
n1.val = 1
n2.val = 2
n3.val = 3
n4.val = 2
n5.val = 1
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
class Solution:
# @param {ListNode} head
# @return {boolean}
def isPalindrome(self, head):
totalLength = self.getChainLength(head)
if totalLength <= 1:
return True
if totalLength == 2:
return head.val == head.next.val