문제 설명 :
Linked List에서 중복된 값을 제거하는 문제
/**
* 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.val == current.next.val){
current.next = current.next.next;
}else{
current = current.next;
}
}
return head;
}
}
해설 :
ListNode에서 현재 val와 next.val를 비교하여 같으면, next를 next.next로 연결해주어 중복을 제거
'Algorithm > LeetCode' 카테고리의 다른 글
[Java] 94. Binary Tree Inorder Traversal (0) | 2023.06.12 |
---|---|
[Java] 88. Merge Sorted Array (0) | 2023.06.12 |
[Java] 70. Climbing Stairs (2) | 2023.06.09 |
[Java] 69. Sqrt(x) (0) | 2023.06.09 |
[Java] 67. Add Binary (0) | 2023.06.08 |