?LeetCode刷題實戰(zhàn)83: 刪除排序鏈表中的重復(fù)元素
算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !
今天和大家聊的問題叫做?刪除排序鏈表中的重復(fù)元素,我們先來看題面:
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
Given a sorted linked list, delete all duplicates such that each element appear only once.
題意
示例 1:
輸入: 1->1->2
輸出: 1->2
示例 2:
輸入: 1->1->2->3->3
輸出: 1->2->3
解題
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;
}
上期推文:
評論
圖片
表情
