JS 알고리즘 13일차 - 단일 연결 리스트(shift, unshift)


JS 알고리즘 13일차 - 단일 연결 리스트(shift, unshift)

Shift 개념 현재 헤드가 가리키고 있는 노드를 제거한 후 헤드를 헤드가 가리키도 있던 리스트의 두 번째 노드를 가리키도록 한다. 의사코드 노드가 없을 경우 undefine을 반환한다. 노드가 존재할 경우 현재의 헤드 속성을 변수에 저장하고 현재 헤드의 next 노드를 가리키도록 헤드 속성을 업데이트 한다. 마지막으로 리스트의 길이를 1만큼 감소시키고 제거된 이전 헤드 노드의 값을 반환하다. 코드 class Node{ constructor(val) { this.val = val; this.next = null; } } class SinglyLinkedList{ constructor(){ this.head = null; this.tail = null; this.length = 0; } shift() { if (!this.head) return undefined; let currentHead = this.head; this.head = currentHead.next; this.leng...


#JavaScript #JS알고리즘 #shift #unshift #단일연결리스트 #알고리즘 #자료구조

원문링크 : JS 알고리즘 13일차 - 단일 연결 리스트(shift, unshift)