题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
说明:本题目包含复杂数据结构ListNode,点此查看相关信息
# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution:def deleteDuplication(self, pHead):# write code hereif not pHead or not pHead.next:return pHeadHead = ListNode(-1)Head.next = pHeadp_back = Headp_front = Head.nextwhile p_front:if p_front.next and p_front.val==p_front.next.val:while p_front.next and p_front.val==p_front.next.val:p_front = p_front.nextp_front = p_front.nextp_back.next = p_frontelse:p_back = p_frontp_front = p_front.nextreturn Head.next
