题目
解答
思路
用 r 记录 head 的找寻不重复元素的过程,当 head.next和 head 相同时,将 head.next 的下一个赋给他,不相同则正常遍历往下寻找。
代码
# 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
"""
r=head
while head:
if head.next and head.next.val==head.val:
head.next=head.next.next
else:
head=head.next
return r