LintCode Python 入门级题目 删除链表元素、整数列表排序

删除链表元素:

  循环列表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()

  

原文地址:https://www.cnblogs.com/bozhou/p/6928694.html