LeetCode Easy:83. Remove Duplicates from Sorted List
一、题目
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
要求是删去有序链表中重复的元素。
二、此题比较简单,设置一个游标遍历链表,只要比较游标的当前和next.value是否相同,相同的话就跳过,让当前游标指向next.next
三、代码
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ p = head while p!= None: while p.next != None and p.val == p.next.val: p.next = p.next.next p = p.next return head
既然无论如何时间都会过去,为什么不选择做些有意义的事情呢