zoukankan      html  css  js  c++  java
  • 83. Remove Duplicates from Sorted List(js)

    83. Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once.

    Example 1:

    Input: 1->1->2
    Output: 1->2
    

    Example 2:

    Input: 1->1->2->3->3
    Output: 1->2->3
    题意:对排好序的链表去重
    代码如下:
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} head
     * @return {ListNode}
     */
    var deleteDuplicates = function(head) {
             let node=head;
            
            while(node){
                if(!node.next){ 
                    break;
                }
                //若相邻两项值相等,跳过,不相等继续下一项
                if(node.val==node.next.val){
                    node.next=node.next.next;
                }else{
                    node=node.next;
                }
                
            }
            return head;
    };
  • 相关阅读:
    coder的脚印
    Mysql
    MSDos
    Windows Develop
    Eclipse 使用总结
    DBA常用SQL
    SSH总结
    Unity3D协程
    yield的作用
    UGUI优化
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/10666904.html
Copyright © 2011-2022 走看看