码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode]题解(python):083 - Remove Duplicates from Sorted List

时间:2016-04-08 11:55:58      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

题目来源


https://leetcode.com/problems/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.


题意分析


Input:

  :type head: ListNode

Output:

  :rtype: ListNode

Conditions:一个有序list,删除掉重复的元素


题目思路


每次判断增加一个节点时,看是否与当前结点重复,重复则跳过


AC代码(Python)

 1 # Definition for singly-linked list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution(object):
 8     def deleteDuplicates(self, head):
 9         """
10         :type head: ListNode
11         :rtype: ListNode
12         """
13         if head == None or head.next == None:
14             return head
15         p = head
16         while p.next != None:
17             if p.val == p.next.val:
18                 p.next = p.next.next
19             else:
20                 p = p.next
21         return head
22             

 

[LeetCode]题解(python):083 - Remove Duplicates from Sorted List

标签:

原文地址:http://www.cnblogs.com/loadofleaf/p/5367040.html

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