Ejemplo n.º 1
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        if l1 is None and l2 is None:
            return None
        pre = result = ListNode(0)
        pl = 0
        while l1 is not None:
            if l2 is not None:
                temp = l1.val + l2.val
                l2 = l2.next
            else:
                temp = l1.val
            div, mod = divmod(temp + pl, 10)
            result.next = ListNode(mod)
            pl = div
            l1 = l1.next
            result = result.next
        while l2 is not None:
            div, mod = divmod(l2.val + pl, 10)
            result.next = ListNode(mod)
            pl = div
            l2 = l2.next
            result = result.next
        if pl != 0:
            result.next = ListNode(pl)

        return pre.next
Ejemplo n.º 2
0
 def mergeTwoLists3(self, l1: ListNode, l2: ListNode) -> ListNode:
     if l1 is None:
         return l2
     elif l2 is None:
         return l1
     elif l1.val < l2.val:
         l1.next = self.mergeTwoLists3(l1.next, l2)
         return l1
     else:
         l2.next = self.mergeTwoLists3(l2.next, l1)
         return l2
Ejemplo n.º 3
0
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if head is None:
            return None

        cur = ListNode(0)
        cur.next = head
        res = cur
        while cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return res.next
Ejemplo n.º 4
0
 def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
     if head is None:
         return None
     res = ListNode(0)
     res.next = head
     l1 = res
     l2 = res
     for i in range(n):
         l1 = l1.next
     while l1.next:
         l1 = l1.next
         l2 = l2.next
     l2.next = l2.next.next
     return res.next
Ejemplo n.º 5
0
 def reverseList_re(self, head: ListNode) -> ListNode:
     if head is None or head.next is None:
         return head
     r = self.reverseList_re(head.next)
     head.next.next = head
     head.next = None
     return r
Ejemplo n.º 6
0
 def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode:
     head = ListNode(0)
     cur = head
     cur1 = l1
     cur2 = l2
     while cur1 and cur2:
         if cur1.val <= cur2.val:
             cur.next = cur1
             cur1 = cur1.next
         else:
             cur.next = cur2
             cur2 = cur2.next
         cur = cur.next
     if cur1:
         cur.next = cur1
     if cur2:
         cur.next = cur2
     return head.next
Ejemplo n.º 7
0
        if head is None or head.next is None:
            return head
        fast = head
        slow = head
        while fast.next is not None and fast.next.next is not None:
            fast = fast.next.next
            slow = slow.next
        if fast.next:
            return slow.next  # 偶数,选第二个
        else:
            return slow

    def middleNode2(self, head: ListNode) -> ListNode:
        if head is None or head.next is None:
            return head
        fast = head
        slow = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow.val


if __name__ == '__main__':
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    head.next.next.next = ListNode(4)
    # head.next.next.next.next = ListNode(5)
    print(Solution().middleNode2(head))
Ejemplo n.º 8
0
   链表的倒数第k个结点
"""

from tencent.linkedlist.ListNode import ListNode


class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
        if head is None or k == 0:
            return None
        fast = head
        slow = head
        for i in range(k):
            if fast:
                fast = fast.next
            else:
                return None  # 长度 < k
        while fast:
            fast = fast.next
            slow = slow.next
        return slow


if __name__ == '__main__':
    head = ListNode(1)
    # head.next = ListNode(2)
    # head.next.next = ListNode(3)
    # head.next.next.next = ListNode(4)
    # head.next.next.next.next = ListNode(5)
    print(Solution().getKthFromEnd(head, 2))
Ejemplo n.º 9
0
 def addTwoNumbers2(self, l1: ListNode, l2: ListNode) -> ListNode:
     if l1 is None or l2 is None:
         return None
     len_l1, len_l2 = 0, 0
     tmp_l1, tmp_l2 = l1, l2
     while tmp_l1:
         tmp_l1 = tmp_l1.next
         len_l1 += 1
     while tmp_l2:
         tmp_l2 = tmp_l2.next
         len_l2 += 1
     if len_l1 < len_l2:
         l1, l2 = l2, l1
     up = 0
     head = ListNode(0)
     i = 0
     while l2:
         res = l1.val + l2.val + up
         up = 0
         if res >= 10:
             up, cur = divmod(res, 10)
         else:
             cur = res
         if i == 0:
             result = ListNode(cur)
             head.next = result
             i += 1
         else:
             result.next = ListNode(cur)
             result = result.next
         l1 = l1.next
         l2 = l2.next
     while l1:
         res = l1.val + up
         up = 0
         up, cur = divmod(res, 10)
         result.next = ListNode(cur)
         result = result.next
         l1 = l1.next
     if up != 0:
         result.next = ListNode(up)
     return head.next
Ejemplo n.º 10
0
            if i == 0:
                result = ListNode(cur)
                head.next = result
                i += 1
            else:
                result.next = ListNode(cur)
                result = result.next
            l1 = l1.next
            l2 = l2.next
        while l1:
            res = l1.val + up
            up = 0
            up, cur = divmod(res, 10)
            result.next = ListNode(cur)
            result = result.next
            l1 = l1.next
        if up != 0:
            result.next = ListNode(up)
        return head.next


if __name__ == '__main__':
    root = ListNode(1)
    # root.next = ListNode(4)
    # root.next.next = ListNode(3)

    root2 = ListNode(9)
    root2.next = ListNode(9)
    # root2.next.next = ListNode(4)
    print(Solution().addTwoNumbers2(root, root2))
Ejemplo n.º 11
0
   移除链表元素
"""
from tencent.linkedlist.ListNode import ListNode


class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if head is None:
            return None

        cur = ListNode(0)
        cur.next = head
        res = cur
        while cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return res.next


if __name__ == '__main__':
    root = ListNode(6)
    root.next = ListNode(2)
    root.next.next = ListNode(6)
    root.next.next.next = ListNode(4)
    root.next.next.next.next = ListNode(5)
    root.next.next.next.next.next = ListNode(6)
    print(Solution().removeElements(root, 6))
Ejemplo n.º 12
0
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     result = ListNode(0)
     self.merge(l1, l2, result)
     return result.next
Ejemplo n.º 13
0
class Solution:
    def reverseList_re(self, head: ListNode) -> ListNode:
        if head is None or head.next is None:
            return head
        r = self.reverseList_re(head.next)
        head.next.next = head
        head.next = None
        return r

    # 构造一个None,然后将所有的node移到
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None or head.next is None:
            return head
        pre = None
        cur = head
        while cur != None:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre


if __name__ == '__main__':
    root = ListNode(1)
    root.next = ListNode(2)
    root.next.next = ListNode(3)
    root.next.next.next = ListNode(4)
    root.next.next.next.next = ListNode(5)
    print(Solution().reverseList(root))
Ejemplo n.º 14
0
   Date:          2020/3/19

   https://leetcode-cn.com/explore/interview/card/tencent/222/linked-list/916/

   链表是否有环
"""
from tencent.linkedlist.ListNode import ListNode


class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if head is None or head.next is None:
            return False
        fast = head
        slow = head

        while True:
            if fast.next is not None and fast.next.next is not None:
                fast = fast.next.next
                slow = slow.next
            else:
                return False
            if slow == fast:
                return True


if __name__ == '__main__':
    root = ListNode(1)
    root.next = ListNode(2)
    print(Solution().hasCycle(root))