83:删除排序链表中的重复元素
This commit is contained in:
parent
415fa76ce6
commit
6c8be326fa
@ -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)
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除所有重复的元素,使每个元素 <strong>只出现一次</strong> 。</p>
|
||||
|
||||
<p>返回同样按升序排列的结果链表。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/list1.jpg" style="width: 302px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,1,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/list2.jpg" style="width: 542px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,1,2,3,3]
|
||||
<strong>输出:</strong>[1,2,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
<li>题目数据保证链表已经按升序排列</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>链表</li></div></div>\n<div><li>👍 583</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user