题目描述
从一个有序链表中,删除重复的元素,使每个元素只出现一次。
解题思路
比较p的值和p的next的值,如果想等,令p.next == p.next.next,否则,令p=p.next
代码
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
p = head
while p:
if p.next and p.val == p.next.val:
p.next = p.next.next
else:
p = p.next
return head