博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 83. Remove Duplicates from Sorted List
阅读量:7099 次
发布时间:2019-06-28

本文共 1426 字,大约阅读时间需要 4 分钟。

 

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

For example,

Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

  

可以用一个指针,也可以用两个指针来完成

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode deleteDuplicates(ListNode head) {        if(head == null){            return null;        }                ListNode cur = head;        while(cur.next != null){            if(cur.val == cur.next.val){                cur.next = cur.next.next;            }else{                cur = cur.next;            }        }        return head;            }}

 

 
/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode deleteDuplicates(ListNode head) {        if(head == null) return head;                ListNode pre = head;        ListNode cur = head.next;                while(cur != null && pre != null){                        if(cur.val == pre.val){                pre.next = cur.next;                cur = cur.next;                            }else{                pre = pre.next;                cur = cur.next;            }        }        return head;                    }}

 

 

 

转载于:https://www.cnblogs.com/iwangzheng/p/5705110.html

你可能感兴趣的文章
Sqlite中使用rowid来表示行号,用于分页。
查看>>
HDU 4916 树形dp
查看>>
远程数据库迁移数据
查看>>
ZH奶酪:LAMP环境中如何重新部署一个Yii2.0 web项目
查看>>
一些有用的java 框架
查看>>
访问不了firefox附加组件页面怎么办
查看>>
Docker image 镜像介绍
查看>>
Java线程池
查看>>
ArrayList,LinkedList,Vector,Stack之间的区别
查看>>
Freemarker常用技巧(二)
查看>>
2.C#中通过委托Func消除重复代码
查看>>
[转] 基于PHP Stream Wrapper开发有趣应用场景
查看>>
JS获取屏幕大小
查看>>
hdu2222-Keywords Search 【AC自动机】
查看>>
Jsp使用HttpSessionBindingListener实现在线人数记录
查看>>
SQL中的等号、IN、LIKE三者的比较
查看>>
JSPatch 成长之路
查看>>
vuejs学习网站推荐
查看>>
如何在Fedora或CentOS上使用Samba共享
查看>>
乐视mysql面试题
查看>>