题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
源代码:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
import java.util.HashMap;
public class Solution {//这道题不对啊
public ListNode deleteDuplication(ListNode pHead)
{
ListNode pre=null;
if(pHead==null||pHead.next==null) return pHead;
HashMap<ListNode,Integer> m=new HashMap<ListNode,Integer>();
m.put(pHead,1);
pre=pHead;
pHead=pHead.next;
while(pHead!=null){
if(m.containsKey(pHead)){
//nextnext=pHead.next;
if(!m.containsKey(pHead.next)){
pre.next=pHead.next;
pHead=pHead.next;
//m.put(pHead,1);
}
}else{
m.put(pHead,1);
pre.next=pHead;
pre=pHead;
pHead=pHead.next;
}
}
return pre;
}
}
708

被折叠的 条评论
为什么被折叠?



