在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
//使用虚拟头结点,方便在链表头部进行的一些操作
ListNode visualHead = new ListNode(-1);
visualHead.next = pHead;
//当前节点的前一个节点
ListNode pre = visualHead;
while(pHead != null && pHead.next != null){
if(pHead.val == pHead.next.val){
int value = pHead.val;
while(pHead != null && pHead.val == value)
pHead = pHead.next;
pre.next = pHead;
}else{
pre = pHead;
pHead = pHead.next;
}
}
return visualHead.next;
}
}