Solution
class ListNode {
int val;
ListNode next;
public ListNode() {
}
public ListNode(int val) {
this.val = val;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public class RemoveDuplicatesFromSortedList {
// The list is guaranteed to be sorted in ascending order.
public static ListNode deleteDuplicates(ListNode head) {
ListNode currentNode = head;
// νμ¬ λ
Έλμ λ€μ λ
Έλκ° nullμ΄ μλ λ
while (currentNode != null && currentNode.next != null) {
if (currentNode.val == currentNode.next.val) { // κ°μ κ²½μ°
currentNode.next = currentNode.next.next; // νμ¬ λ
Έλκ° λ€λ€μ λ
Έλλ₯Ό κ°λ¦¬ν€κ² ν¨
} else { // κ°μ§ μμ κ²½μ°
currentNode = currentNode.next; // λ€μ λ
Έλλ‘ μ΄λ
}
}
return head; // pointer
}
}
Leave a comment