Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]
生活随笔
收集整理的這篇文章主要介紹了
Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
翻譯:
給定一個排序號的鏈表,刪除所有的重復元素,保證每個元素只出現一次。
分析:
在當前節點刪除下一節點,會比較容易操作,只需要修改next指針。
代碼:
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/ public class Solution {public ListNode deleteDuplicates(ListNode head) {if(head==null){return null;}ListNode currentNode=head;while(currentNode.next!=null){if(currentNode.next.val==currentNode.val){currentNode.next=currentNode.next.next;}else{currentNode=currentNode.next;}}return head;} }總結
以上是生活随笔為你收集整理的Leet Code OJ 83. Remove Duplicates from Sorted List [Difficulty: Easy]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leet Code OJ 21. Mer
- 下一篇: Leet Code OJ 27. Rem