题目描述

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
说明:本题目包含复杂数据结构ListNode,点此查看相关信息

  1. # -*- coding:utf-8 -*-
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution:
  7. def deleteDuplication(self, pHead):
  8. # write code here
  9. if not pHead or not pHead.next:
  10. return pHead
  11. Head = ListNode(-1)
  12. Head.next = pHead
  13. p_back = Head
  14. p_front = Head.next
  15. while p_front:
  16. if p_front.next and p_front.val==p_front.next.val:
  17. while p_front.next and p_front.val==p_front.next.val:
  18. p_front = p_front.next
  19. p_front = p_front.next
  20. p_back.next = p_front
  21. else:
  22. p_back = p_front
  23. p_front = p_front.next
  24. return Head.next