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