删除链表元素:

  循环列表head,判断当前指针pre.next的val是否等于val,

  如果是,当前pre重指向pre.next.next,

  直至pre.next = Null

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @param val, an integer
    # @return a ListNode
    def removeElements(self, head, val):
        # Write your code here
        if head is None:
            return None
        while head.val ==val:
            head = head.next
            if (head == None):
                return None
        pre = head
        while pre.next is not None:
            if pre.next.val == val:
                pre.next = pre.next.next
            else:
                pre = pre.next
        return head
  

整数列表排序:

  Python的列表包含sort()方法,可以直接对列表排序并返回排序好之后的列表;
如需逆序排序,先sort后再调用reverse逆序即可。

class Solution:
    # @param {int[]} A an integer array
    # @return nothing
    def sortIntegers(self, A):
        return A.sort()

  

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2021-10-16
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-09-27
  • 2022-12-23
  • 2022-02-02
相关资源
相似解决方案