示例#1
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     stk1, stk2 = [], []
     # 放入栈中
     while l1 or l2:
         if l1:
             stk1.append(l1.val)
             l1 = l1.next
         if l2:
             stk2.append(l2.val)
             l2 = l2.next
     dummy = ListNode(-1)
     carry = 0
     # 对栈中元素进行相加
     while stk1 or stk2:
         val = carry
         if stk1:
             val += stk1.pop()
         if stk2:
             val += stk2.pop()
         carry, val = divmod(val, 10)
         # 头插
         node = ListNode(val)
         node.next = dummy.next
         dummy.next = node
     # 处理最后一次进位
     if carry:
         node = ListNode(1)
         node.next = dummy.next
         dummy.next = node
     return dummy.next
示例#2
0
 def reverseList2(self, head):
     dummy = ListNode(-1)
     while head:
         _next = head.next
         head.next = dummy.next
         dummy.next = head
         head = _next
     return dummy.next
示例#3
0
 def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
     dummy = ListNode(-1)
     dummy.next = head
     slow = fast = dummy
     for i in range(n + 1):
         fast = fast.next
     while fast:
         slow = slow.next
         fast = fast.next
     # slow.next即为被删节点,不为None
     slow.next = slow.next.next
     return dummy.next
示例#4
0
 def removeNthFromEnd(self, head, n):
     """
     :type head: ListNode
     :type n: int
     :rtype: ListNode
     """
     dummy = ListNode(-1)
     dummy.next = head
     ptr1 = dummy
     ptr2 = head
     for i in range(n):
         ptr2 = ptr2.next
     while ptr2:
         ptr1 = ptr1.next
         ptr2 = ptr2.next
     ptr1.next = ptr1.next.next
     return dummy.next
示例#5
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     dummy = ListNode(-1)
     ptr = dummy
     carry = 0
     while l1 or l2:
         val = carry
         if l1:
             val += l1.val
             l1 = l1.next
         if l2:
             val += l2.val
             l2 = l2.next
         carry, val = divmod(val, 10)
         ptr.next = ListNode(val)
         ptr = ptr.next
     # 最后一次进位,总是错!!!
     if carry:
         ptr.next = ListNode(1)
     return dummy.next
示例#6
0
 def reverseList(self, head: ListNode) -> ListNode:
     if not head or not head.next:
         return head
     # 先递归,reverse head.next开头的链表,返回了将来的头结点
     # head.next在这个过程中,没有被修改
     node = self.reverseList(head.next)
     # 这里才真正reverse
     head.next.next = head
     head.next = None
     return node
示例#7
0
        :type n: int
        :rtype: ListNode
        """
        dummy = ListNode(-1)
        dummy.next = head
        ptr1 = dummy
        ptr2 = head
        for i in range(n):
            ptr2 = ptr2.next
        while ptr2:
            ptr1 = ptr1.next
            ptr2 = ptr2.next
        ptr1.next = ptr1.next.next
        return dummy.next


node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(4)
node5 = ListNode(5)

node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5

Solution().removeNthFromEnd(node1, 5)

a = 1
示例#8
0
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
from util.List import ListNode


class Solution(object):
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next


node1 = ListNode(4)
node2 = ListNode(5)
node3 = ListNode(1)
node4 = ListNode(9)

node1.next = node2
node2.next = node3
node3.next = node4

Solution().deleteNode(node2)

a = 1