13. Delete the sort the list of repeating elements

Subject description:

Given a sorted list, delete all the duplicate elements, so that each element occurs only once.
Example 1:

输入: 1->1->2
输出: 1->2

Example 2:

输入: 1->1->2->3->3
输出: 1->2->3

Code:

JavaScript

  • Relatively simple, familiar list.
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    var cur = head
    while(cur && cur.next) {
        if (cur.next.val === cur.val) {
            cur.next = cur.next.next
        } else {
            cur = cur.next
        }       
    }
    return head
};

Here Insert Picture Description

Published 14 original articles · won praise 1 · views 1696

Guess you like

Origin blog.csdn.net/weixin_45569004/article/details/104728623