本文共 771 字,大约阅读时间需要 2 分钟。
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
思路:需要三个指针,第一个指针first的next指向头结点,第二个指针pre指向first,确保每次指向的是重复结点的前一个结点,第三个指针head来遍历,遇到重复结点,则向后,直到找到当前结点不等于下一个结点时,将pre的next指向head。
function deleteDuplication(pHead){ // write code here if(pHead==null||pHead.next==null) return pHead; var first={ val:0, next:pHead } var head=pHead,pre=first; while(head!=null&&head.next!=null){ if(head.val==head.next.val){ while(head.next&&head.next.val==head.val){ head=head.next; } pre.next=head.next; }else{ pre.next=head; pre=pre.next; } //pre=head; head=head.next; } return first.next;}
转载地址:http://iwxgj.baihongyu.com/