本文主要是介绍Remove Duplicates from Sorted List - LeetCode,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Remove Duplicates from Sorted List - LeetCode
题目:
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
.
分析:
代码:
class Solution:# @param head, a ListNode# @return a ListNodedef deleteDuplicates(self, head):now = 0prev = headtemp = headwhile head is not None:if now != head.val:now = head.valprev = headhead = head.nextelse:head = self.delete(prev, head)return tempdef delete(self,prev,head):prev.next = head.nextdel headreturn prev.next
这篇关于Remove Duplicates from Sorted List - LeetCode的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!