标签:nbsp else lse int static ted ret 出现 代码
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2
输出: 1->2
示例 2:
输入: 1->1->2->3->3
输出: 1->2->3
代码如下:
public class LeetCode83 { public static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) { return head; } ListNode prev = head; ListNode cur = head.next; while (cur != null) { if (prev.val == cur.val) { prev.next = cur.next; } else { prev = prev.next; cur = cur.next; } } return head; } }
标签:nbsp else lse int static ted ret 出现 代码
原文地址:https://www.cnblogs.com/Booker808-java/p/9162254.html