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
.
c++实现
ListNode* deleteDuplicates(ListNode* head) {
ListNode* cur = head;
while(cur) {
while(cur->next && cur->val == cur->next->val) {
cur->next = cur->next->next;
}
cur = cur->next;
}
return head;
}
python实现
# 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
"""
p = head
while p and p.next:
if p.val == p.next.val:
p.next = p.next.next
else:
p = p.next
return head