代码拉取完成,页面将自动刷新
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;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。