diff --git a/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedList.java b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedList.java new file mode 100644 index 0000000..85634ea --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedList.java @@ -0,0 +1,70 @@ +//存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次 。 +// +// 返回同样按升序排列的结果链表。 +// +// +// +// 示例 1: +// +// +//输入:head = [1,1,2] +//输出:[1,2] +// +// +// 示例 2: +// +// +//输入:head = [1,1,2,3,3] +//输出:[1,2,3] +// +// +// +// +// 提示: +// +// +// 链表中节点数目在范围 [0, 300] 内 +// -100 <= Node.val <= 100 +// 题目数据保证链表已经按升序排列 +// +// Related Topics 链表 +// 👍 583 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//83:删除排序链表中的重复元素 +public class RemoveDuplicatesFromSortedList{ + public static void main(String[] args) { + //测试代码 + Solution solution = new RemoveDuplicatesFromSortedList().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode deleteDuplicates(ListNode head) { + ListNode temp = head; + while (temp!=null&&temp.next!=null){ + if (temp.next.val == temp.val) { + temp.next = temp.next.next; + } else { + temp = temp.next; + } + } + return head; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedList.md b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedList.md new file mode 100644 index 0000000..1f92f4a --- /dev/null +++ b/src/main/java/leetcode/editor/cn/RemoveDuplicatesFromSortedList.md @@ -0,0 +1,30 @@ +

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

+ +

返回同样按升序排列的结果链表。

+ +

 

+ +

示例 1:

+ +
+输入:head = [1,1,2]
+输出:[1,2]
+
+ +

示例 2:

+ +
+输入:head = [1,1,2,3,3]
+输出:[1,2,3]
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 链表
  • \n
  • 👍 583
  • 👎 0
  • \ No newline at end of file