码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode83 Remove Duplicates from Sorted List

时间:2020-02-07 19:14:50      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:out   定义   leetcode   sorted   else   each   init   删除   ini   

 1 """
 2 Given a sorted linked list, delete all duplicates such that each element appear only once.
 3 Example 1:
 4 Input: 1->1->2
 5 Output: 1->2
 6 Example 2:
 7 Input: 1->1->2->3->3
 8 Output: 1->2->3
 9 """
10 """
11 删除链表结点,操作这类的题,一般需要定义两个指针
12 分别指向head 和 head.next 然后画图总结规律
13 """
14 class ListNode:
15     def __init__(self, x):
16         self.val = x
17         self.next = None
18 
19 class Solution:
20     def deleteDuplicates(self, head):
21         if head == None:
22             return head
23         p = head
24         q = head.next
25         while q:
26             if p.val == q.val:
27                 p.next = q.next
28                 q = p.next
29             else:
30                 p = p.next
31                 q = q.next
32         return head

 

leetcode83 Remove Duplicates from Sorted List

标签:out   定义   leetcode   sorted   else   each   init   删除   ini   

原文地址:https://www.cnblogs.com/yawenw/p/12273841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!