Ai
1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_83.java 1.01 KB
一键复制 编辑 原始数据 按行查看 历史
Steve Sun 提交于 2018-02-20 23:29 +08:00 . refactor 83
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
/**
* 83. Remove Duplicates from Sorted List
*
* 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.
*/
public class _83 {
public static class Solution1 {
public ListNode deleteDuplicates(ListNode head) {
ListNode ret = new ListNode(-1);
ret.next = head;
while (head != null) {
while (head.next != null && head.next.val == head.val) {
head.next = head.next.next;
}
head = head.next;
}
return ret.next;
}
}
public static class Solution2 {
public ListNode deleteDuplicates(ListNode head) {
ListNode curr = head;
while (curr != null && curr.next != null) {
if (curr.val == curr.next.val) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}
return head;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助