Leetcode刷题笔记-linklist

82. Remove Duplicates from Sorted List II

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = pre = ListNode(111)
        dummy.next = head
        
        while head and head.next:
            
            if head.val == head.next.val:
                while head and head.next and head.val == head.next.val:
                    head = head.next
                    
                head = head.next
                pre.next = head
            else:
                pre = pre.next
                head = head.next
                
        return dummy.next

 

369. Plus One Linked List

p1指向最后面非9的数字。p2指向结尾。  

class Solution(object):
    def plusOne(self, head):
        # two pointers
        dummy = ListNode(0)
        dummy.next = head
        p1 = p2 = dummy
        
        while p2.next:
            p2 = p2.next
            if p2.val != 9:
                p1 = p2
        
        if p2.val != 9:
            p2.val += 1
        else:
            p1.val += 1
            while p1.next:
                p1.next.val = 0
                p1 = p1.next
            
        if dummy.val != 0:
            return dummy
        else:
            return dummy.next

109. Convert Sorted List to Binary Search Tree

class Solution(object):
    def sortedListToBST(self, head):
        def helper(first, tail):
            # if not first: return None  # 注意,这里是错误的
            if first == tail: return None
            fast = slow = first
            while fast != tail and fast.next != tail:
                slow = slow.next
                fast = fast.next.next
            root = TreeNode(slow.val)
            root.left = helper(first, slow)
            root.right = helper(slow.next, tail)
            return root
        return helper(head, None)

986. Interval List Intersections

class Solution(object):
    def intervalIntersection(self, A, B):
        i = j = 0
        res = []
        
        while i < len(A) and j < len(B):
            # 没有overlap
            if A[i].start > B[j].end:
                j += 1
            elif B[j].start > A[i].end:
                i += 1
            else:  # overlap
                res.append(Interval(max(A[i].start, B[j].start), min(A[i].end, B[j].end)))
                if A[i].end < B[j].end:
                    i += 1
                else:
                    j += 1
        return res

445. Add Two Numbers II

用堆栈做

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        stack1, stack2 = [], []
        while l1:
            stack1.append(l1.val)
            l1 = l1.next
        
        while l2:
            stack2.append(l2.val)
            l2 = l2.next
            
        nextNode = None
        overflow = 0
        while stack1 or stack2 or overflow:
            val1 = stack1.pop() if stack1 else 0
            val2 = stack2.pop() if stack2 else 0
            val = val1+val2+overflow
            node = ListNode(val % 10)
            overflow = val / 10
            node.next = nextNode
            nextNode = node
        return node

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值