题解 | #15.删除有序链表中重复的元素-I#

删除有序链表中重复的元素-I

http://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79

function deleteDuplicates( head ) {
  if(head==null || head.next==null)  
    return head;
  
  let cur = head;
  while(cur.next != null){
    if(cur.val == cur.next.val){
      cur.next = cur.next.next;
    }else{
      cur = cur.next;
    }
  }
  return head;
}
/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @return ListNode类
  */
 function deleteDuplicates( head ) {
  let empty = new ListNode(-100),
      pre = empty;
  
  while(head){
   if(head.val != pre.val){
     pre.next = head;
     pre = pre.next;
   }
    head = head.next;
  }
  
  pre.next = null;
  return empty.next;
}

全部评论

相关推荐

身边有人上海、深圳 6、7k 都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务