剑指offer18:删除链表节点_双指针

1.考虑输入空链表和一个节点链表

2.如果头节点不重复,直接递归查找重复

3.双指针,进行判断两个节点是不是相等

# -*- 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 pHead is None or pHead.next is None:
return pHead
head1 = pHead.next
if head1.val != pHead.val:
pHead.next = self.deleteDuplication(pHead.next)
else:
while pHead.val == head1.val and head1.next is not None:
head1 = head1.next
if head1.val != pHead.val:
pHead = self.deleteDuplication(head1)
else:
return None
return pHead