题解 | #删除有序链表中重复的元素-I#
删除有序链表中重复的元素-I
https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类
# @return ListNode类
#
class Solution:
def deleteDuplicates(self , head: ListNode) ->ListNode:
#判断链表是否为空
if head == None:
return None
#遍历链表 转化为数组
p = head
nums = []
while p != None :
nums.append(p.val)
p = p.next
new1 = []
#遍历数组去重
for i in nums:
if i not in new1:
new1.append(i)
#构建虚拟节点 将数组转为链表
p1 = ListNode(-1)
p2 = p1
for i in new1:
temp = ListNode(i)
p2.next = temp
p2 = p2.next
return p1.next
查看14道真题和解析
曼迪匹艾公司福利 95人发布