Python代码,时间复杂度O(N),空间复杂度O(1)

删除链表中重复的结点

http://www.nowcoder.com/questionTerminal/fc533c45b73a41b0b44ccba763f866ef

class Solution:
def deleteDuplication(self, pHead):
# write code here
if not pHead or not pHead.next:return pHead
head = ListNode(0)
head.next = pHead
pre = head
last = pre.next
while last:
if (last.next and last.val == last.next.val):
while last.next and last.val == last.next.val:
last = last.next
pre.next = last.next
last = pre.next
else:
pre = last
last = last.next
return head.next

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务