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;
}
}
相關文章
- Remove Duplicates from Sorted List 去除連結串列中重複值節點REM
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted ListREM
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- Remove-duplicates-from-sorted-listREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- 【Leetcode】83. Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode-Remove Duplicates from Sorted List IILeetCodeREM
- Remove Duplicates from Sorted List II leetcode javaREMLeetCodeJava
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除無序連結串列中的重複項REM
- 83. Remove Duplicates from Sorted ListREM
- 【Leetcode】82. Remove Duplicates from Sorted List IILeetCodeREM
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 82. Remove Duplicates from Sorted List IIREM
- 【LeetCode 234_連結串列】Palindrome Linked ListLeetCode
- Linked List Cycle leetcode java (連結串列檢測環)LeetCodeJava
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- [LeetCode] Remove Duplicates from Sorted Array 有序陣列中去除重複項LeetCodeREM陣列
- Linked List Cycle leetcode II java (尋找連結串列環的入口)LeetCodeJava
- [LeetCode Python 3] 876. Middle of the Linked List(連結串列的中間結點)LeetCodePython
- Chapter 2 | Linked Lists--移除未排序連結串列中的重複項APT排序
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- 【小白學演算法】5.連結串列(linked list)、連結串列的新增演算法
- 從未排序的連結串列中刪除重複項排序
- 【資料結構與演算法】——連結串列(Linked List)資料結構演算法
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- 既然已經有陣列了,為什麼還要連結串列?JS連結串列(Linked-list)詳解陣列JS
- 【LeetCode】 Rotate List 迴圈連結串列LeetCode
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- 演算法與資料結構基礎 - 連結串列(Linked List)演算法資料結構
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列