인프런 커뮤니티 질문&답변
답변 2
0
0
감자
지식공유자
insertAt(index, data){
if(index > this.count || index < 0){
throw new Error("범위를 넘어갔습니다.");
}
let newNode = new Node(data);
if(index == 0){
newNode.next = this.head;
this.head = newNode;
} else {
let currentNode = this.head;
for(let i = 0; i < index - 1; i++){
currentNode = currentNode.next;
}
newNode.next = currentNode.next;
currentNode.next = newNode;
}
this.count++;
}
insertAt() 함수에서 마지막줄 말씀하시는게 맞나요?
여기라고 생각하고 답변드리겠습니다!
맞습니다.
if문(가장 앞에 삽입할 때)과 else문(그 외 나머지 위치에 삽입할 때) 다음에 count++를 해줘서 이 연결리스트에 데이터의 수(count)가 하나 늘어났다고 기록해줍니다.





