本题是简单的链表题。
1.思路
1)单指针,下一个和上一个相同,p.next指向p.next.next,否则p指针后移
这里不用哑节点,直接开始就判断
2.代码
# 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
"""
if head == None or head.next == None:
return head
p = head
while p.next:
if p.val == p.next.val:
p.next = p.next.next
else:
p = p.next
return head