1. ๋ฌธ์ : https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
2. ํ์ด
ํ์ฌ๊ฐ๊ณผ ๋ค์ ๊ฐ์ด ๊ฐ์ผ๋ฉด ์ปค์ ๊ณ์ ์ด๋ํด์ฃผ๊ณ
๋ค์ ๊ฐ๊ณผ ๋ค๋ฅผ๋ ์ด์ ๊ฐ์ next์ ์ง๊ธ์ด ๊ฐ์์ง ํ์ธํด์ฃผ๊ณ
๊ฐ๋ค๋ฉด ๋ค์๊ฐ & ํ์ฌ๊ฐ ๋ค์์ผ๋ก ๋๊ธฐ๊ณ , ๋ค๋ฅด๋ฉด ๋ค์๊ฐ์ next๋ฅผ ํ์ฌ๊ฐ์ next๋ก ๋ง์ถฐ์ค๋ค.
3. ์ฝ๋
/**
* 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; }
* }
*/
import java.util.*;
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dHead = new ListNode(-1);
dHead.next = head;
ListNode prev = dHead;
ListNode cur = head;
while(cur != null){
while(cur.next!=null && cur.val==cur.next.val){
cur=cur.next;
}
if(prev.next == cur){
prev=prev.next;
} else{
prev.next=cur.next;
}
cur = cur.next;
}
return dHead.next;
}
}
'๐ป Coding Problems Solving > Two Pointers | Binary Search| LinkedList' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 2512] ์์ฐ (0) | 2023.08.27 |
---|---|
[LeetCode] Add Two Numbers (0) | 2023.07.13 |
[LeetCode] Linked List Cycle II (0) | 2023.07.10 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฐ์๋ ๋ถ๋ถ ์์ด์ ํฉ (์๋ฐ java) (0) | 2023.05.15 |
[LeetCode] 3Sum (0) | 2023.04.12 |
์ต๊ทผ๋๊ธ