head를 return 하는게 적절할지 확신이 없었다.current를 포인터로 활용하면서, 동시에 재할당도 해줬는데 이게 가능한 이유는 head 참조값을 할당했기 때문에 원본도 수정되기 때문.head를 반환해야 하는 이유
head가 전체 리스트의 시작점의 역할을 하기 때문이다.
current의 역할 : 리스트 순회용 포인터
⇒ 결국 current가 포인터로써 참조를 통해 모든 변경사항이 저장됨.
// head : [1 -> 2 -> 2]
const deleteDuplicates = function(head) {
let current = head;
while (current && current.next) {
if (current.val === current.next.val) {
console.log(head); // ✅ [1 -> 2 -> 2]
current.next = current.next.next;
console.log(head); // ✅ [1 -> 2]
// head를 참조하기 있기 때문에 head.next에 재할당 발생
//...
};