删除链表中重复的元素
删除有序链表中重复的元素
http://www.nowcoder.com/questionTerminal/c087914fae584da886a0091e877f2c79
双指针
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self , head):
curp = head
# 不使用 curp.next, 避免越界判断
nextp = head
while curp and nextp:
if curp.val == nextp.val:
# 删除
curp.next = nextp.next
else:
# 继续向后遍历
curp = curp.next
nextp = nextp.next
return head
查看14道真题和解析
阿里巴巴公司氛围 651人发布