python删除链表中的最小元素_LintCode Python 入门级题目 删除链表元素、整数列表排序...
刪除鏈表元素:
循環(huán)列表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
整數(shù)列表排序:
Python的列表包含sort()方法,可以直接對列表排序并返回排序好之后的列表;
如需逆序排序,先sort后再調(diào)用reverse逆序即可。
class Solution:
# @param {int[]} A an integer array
# @return nothing
def sortIntegers(self, A):
return A.sort()
總結(jié)
以上是生活随笔為你收集整理的python删除链表中的最小元素_LintCode Python 入门级题目 删除链表元素、整数列表排序...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中文整合包_案例 | 美研市场营销和整合
- 下一篇: c51为啥要宏定义时钟_51单片机时钟实