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


Python ListNode.val方法代码示例

本文整理汇总了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
开发者ID:arunraman,项目名称:Code-Katas,代码行数:25,代码来源:swap_nodes_linked_list.py

示例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):
开发者ID:leochen4891,项目名称:interviewquestions,代码行数:33,代码来源:ReverseLinkedList.py

示例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
开发者ID:leochen4891,项目名称:interviewquestions,代码行数:33,代码来源:PalindromeLinkedList.py


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