示例#1
0
def delete_middle_node(node: ListNode):
    """
    Implement an algorithm to delete a node in the middle of
    a singly linked list, given only access to that node
    """

    node.val = node.next.val
    node.next = node.next.next
示例#2
0
 def deleteNode(self, node: ListNode):
     """
     :type node: ListNode
     :rtype: void Do not return anything, modify node in-place instead.
     """
     if node.next:
         node.val = node.next.val
         node.next = node.next.next
     else:
         node = None
    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
__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):
 def deleteNode(self, node: ListNode):
     """
     Do not return anything, modify node in-place instead.
     """
     node.val = node.next.val
     node.next = node.next.next
__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