56-删除链表中重复的节点

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

  1. 首先添加一个头节点,以方便碰到第一个,第二个节点就相同的情况
  2. 设置 pre ,last 指针, pre指针指向当前确定不重复的那个节点,而last指针相当于工作指针,一直往后面搜索。
/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function deleteDuplication(pHead)
{
    // write code here
    if(pHead == null || pHead.next == null) return pHead;
    var head = new ListNode(0);
    head.next = pHead;
    var pre = head, last = head.next;
    while(last != null) {
        if(last.next != null && last.val == last.next.val) {
            while(last.next != null && last.val == last.next.val) {
                last = last.next;
            }
            pre.next = last.next;
            last = last.next
        } else {
            pre = pre.next;
            last = last.next;
        }
    }
    return head.next
}
发布了80 篇原创文章 · 获赞 0 · 访问量 1965

猜你喜欢

转载自blog.csdn.net/weixin_43655631/article/details/104098137
今日推荐