题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表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 = None
class Solution:
def deleteDuplication(self, pHead):
# write code here
if not pHead or not pHead.next:
return pHead
Head = ListNode(-1)
Head.next = pHead
p_back = Head
p_front = Head.next
while 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.next
p_front = p_front.next
p_back.next = p_front
else:
p_back = p_front
p_front = p_front.next
return Head.next