Esempio n. 1
0
from python3.aatool.LinkT import ListNode, createSLink


# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        root, pre, x = None, None, 0
        while l1 or l2 or x != 0:
            v1 = l1.val if l1 else 0
            v2 = l2.val if l2 else 0
            x, y = divmod(v1 + v2 + x, 10)
            node = ListNode(y, None)
            if pre:
                pre.next = node
            else:
                root = node
            pre = node
            l1 = l1.next if l1 else l1
            l2 = l2.next if l2 else l2
        return root


# @lc code=end

Solution().addTwoNumbers(createSLink([9, 9, 9, 9, 9, 9, 9]),
                         createSLink([9, 9, 9, 9]))
Esempio n. 2
0
            tailNode = self.recursion(head, head.next)
            # head.next 保存新的头节点
            preNode.next = head.next
            # 单前节点移到尾部
            tailNode.next, head.next = head, tailNode.next
            return tailNode.next
        else:
            return head

    # 迭代
    def iteration(self, head:ListNode) -> ListNode:
        # 空链,单节点链直接返回
        if not head or not head.next: return head
        # 从第二个节点开始遍历
        headNode, tailNode, curNode = head, head, head.next
        while curNode:
            # 当前节点后一个节点移到最新尾部,最新表头移到当前节点后
            tailNode.next, curNode.next = curNode.next, headNode
            # 更新最新节点&单前节点
            headNode, curNode = curNode, tailNode.next
        return headNode

    def reverseList(self, head: ListNode) -> ListNode:
        return self.iteration(head)
        # dummyNode = ListNode(val=0, next=head)
        # self.recursion(dummyNode, head)
        # return dummyNode.next
# @lc code=end

Solution().reverseList(createSLink([1,2,3,4,5]))
    # 反转链表的k个节点并返回反转后最后一个节点
    def reverse(self, preNode: ListNode, head: ListNode, k: int) -> ListNode:
        temp, stack = head, []
        for i in range(k):
            if temp:
                stack.append(temp)
                temp = temp.next
            else:
                break
        if len(stack) < k:
            return None
        for i in range(k-1, -1, -1):
            preNode.next = stack[i]
            preNode = preNode.next
            if i == k-1:
                lstNode = stack[i].next
            if i == 0:
                stack[i].next = lstNode
        return preNode

    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        dummyNode = ListNode(val=0, next=head)
        preNode = dummyNode
        while preNode:
            preNode = self.reverse(preNode, head, k)
            head = preNode.next if preNode else None
        return dummyNode.next
# @lc code=end

Solution().reverseKGroup(createSLink([1,2,3,4,5]), 1)
    #     curMap, preMap, preNode, tmp = [], [], None, head
    #     while tmp:
    #         curMap.append(tmp)
    #         preMap.append(preNode)
    #         preNode = tmp
    #         tmp = tmp.next
    #     dIndex = len(curMap) - n
    #     curNode = curMap[dIndex]
    #     preNode = preMap[dIndex]
    #     if preNode:
    #         preNode.next = curNode.next
    #     else:
    #         head = head.next
    #     return head
    def removeNthFromEnd(self, head, n):
        def index(node):
            if not node:
                return 0
            i = index(node.next) + 1
            if i > n:
                node.next.val = node.val
            return i

        index(head)
        return head.next


# @lc code=end

Solution().removeNthFromEnd(createSLink([1, 2, 3, 4, 5]), 2)
Esempio n. 5
0
#
# @lc app=leetcode.cn id=203 lang=python3
#
# [203] 移除链表元素
#

from python3.aatool.LinkT import ListNode, createSLink


# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        dummyNode = ListNode(val=0, next=head)
        curNode = dummyNode
        while curNode.next:
            if curNode.next.val == val:
                curNode.next = curNode.next.next
            else:
                curNode = curNode.next
        return dummyNode.next


# @lc code=end

Solution().removeElements(createSLink([7, 7, 7, 7]), 7)
Esempio n. 6
0
        while curNode:
            tailNode.next, curNode.next = curNode.next, headNode
            headNode, curNode = curNode, tailNode.next
        return headNode

    def isPalindrome(self, head: ListNode) -> bool:
        # self.head2 = head
        # return self.recursion(head)

        if not head or not head.next: return True
        # 找出中间节点,奇数点归前半段
        slow, fast = head, head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
        # 反转后续节点
        rehead = self.reverseList(slow.next)
        # 判断回文
        flag, head2 = True, rehead
        while head2 and flag:
            if head.val != head2.val: flag = False
            head, head2 = head.next, head2.next
        # 反转回去
        slow.next = self.reverseList(rehead)
        return flag


# @lc code=end

Solution().isPalindrome(createSLink([1, 2, 3, 2, 1]))