Q:
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
.
A:
# 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:
while p.next and p.val == p.next.val:
p.next = p.next.next
p = p.next
return head
关键代码:
while p.next and p.val == p.next.val:
p.next = p.next.next
因为会存在多个节点值相同,所以需要循环检查。