LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked List
1.Description
Given a sorted linked list, delete all duplicates such that each element appear only once.
2.Example
Input: 1->1->2
Output: 1->2
3.Solution
感覺這裡比較繞的是while迴圈的條件,current != null是為了除去head是空的情況,current.next != null是為了保證current.next.next能取到值,至少可以取到一個null。
/**
* 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 current = head;
while (current != null && current.next != null) {
if (current.next.val == current.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
}
}
相關文章
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- Remove-duplicates-from-sorted-listREM
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- [LeetCode Python 3] 876. Middle of the Linked List(連結串列的中間結點)LeetCodePython
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- 【資料結構與演算法】——連結串列(Linked List)資料結構演算法
- 【小白學演算法】5.連結串列(linked list)、連結串列的新增演算法
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- LeetCode 之 JavaScript 解答第141題 —— 環形連結串列 I(Linked List Cycle I)LeetCodeJavaScript
- 既然已經有陣列了,為什麼還要連結串列?JS連結串列(Linked-list)詳解陣列JS
- LeetCode | 141 linked list cycleLeetCode
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- 從未排序的連結串列中刪除重複項排序
- 演算法與資料結構基礎 - 連結串列(Linked List)演算法資料結構
- Leetcode 206. Reverse Linked ListLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- Leetcode 234. Palindrome Linked ListLeetCode
- leetcode-82:刪除排序連結串列中重複的元素-iiLeetCode排序
- 演算法與資料結構-連結串列((linked-list)-Java實現單向連結串列演算法資料結構Java
- Leetcode 142. Linked List Cycle IILeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- LeetCode 138:複製帶隨機指標的連結串列 Copy List with Random PointerLeetCode隨機指標random
- leetcode-019-刪除連結串列倒數第N個結點(Remove Nth Node From End of List)LeetCodeREM
- C++ STL list連結串列C++
- Remove-duplicates-from-sorted-arrayREM
- 資料結構與演算法——連結串列 Linked List(單連結串列、雙向連結串列、單向環形連結串列-Josephu 問題)資料結構演算法
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- linux核心原始碼 -- list連結串列Linux原始碼
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM