题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表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 pre = null;
ListNode cur = pHead;
ListNode nex = null;
while(cur!=null){
boolean flag = false;
nex = cur.next;
while(nex!=null&&nex.val==cur.val){
flag = true;
nex = nex.next;
}
if(flag){
if(pre!=null){
pre.next = nex;
}else{
pHead = null;
}
}else{
if(pre==null){
pHead = cur;
}
pre = cur;
}
cur = nex;
}
return pHead;
}
}