Skip to content

Commit

Permalink
remove duplicates from sorted list easy
Browse files Browse the repository at this point in the history
  • Loading branch information
weiy committed Oct 16, 2018
1 parent ef96aad commit 7a4d26c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Array/RemoveDuplicatesFromSortedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
删除链表中重复的节点。
一直迭代,若当前 val == next.val,则把 next 与 next.next 相连。
beat
100%
测试地址:
https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/
"""
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return head

x = head

while head.next:
if head.val == head.next.val:
head.next = head.next.next
else:
head = head.next

return x

0 comments on commit 7a4d26c

Please sign in to comment.