๐์ค๋ ๋ฐฐ์ด ๊ฒ
๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ตฌํ
class Node{
constructor(val){
this.val = val;
this.next = null;
}
}
class SinglyLinkedList{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(val){
const newNode = new Node(val);
if(!this.head){
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
}
pop(){
if(!this.head) return undefined;
let current = this.head;
let newTail = current;
while(current.next){
newTail = current;
current = current.next;
}
this.tail = newTail;
this.tail.next = null;
this.length--;
if(this.length === 0){
this.head = null;
this.tail = null;
}
return current;
}
shift(){
if(!this.head) return undefined;
const currentHead = this.head;
this.head = currentHead.next;
this.length--;
if(this.length === 0){
this.tail = null;
}
return currentHead;
}
unshift(val){
const newNode = new Node(val);
if(!this.head) {
this.head = newNode;
this.tail = this.head;
}
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
get(index){
if(index < 0 || index >= this.length) return null;
let counter = 0;
let current = this.head;
while(counter !== index){
current = current.next;
counter++;
}
return current;
}
set(index, val){
let foundNode = this.get(index);
if(foundNode){
foundNode.val = val;
return true;
}
return false;
}
insert(index, val){
if(index < 0 || index > this.length) return false;
if(index === this.length) return !!this.push(val);
if(index === 0) return !!this.unshift(val);
const newNode = new Node(val);
const prev = this.get(index - 1);
const temp = prev.next;
prev.next = newNode;
newNode.next = temp;
this.length++;
return true;
}
remove(index){
if(index < 0 || index >= this.length) return undefined;
if(index === 0) return this.shift();
if(index === this.length - 1) return this.pop();
const previousNode = this.get(index - 1);
const removed = previousNode.next;
previousNode.next = removed.next;
this.length--;
return removed;
}
reverse(){
let node = this.head;
this.head = this.tail;
this.tail = node;
let next;
let prev = null;
for(var i = 0; i < this.length; i++){
next = node.next;
node.next = prev;
prev = node;
node = next;
}
return this;
}
print(){
const arr = [];
let current = this.head
while(current){
arr.push(current.val)
current = current.next
}
console.log(arr);
}
}
๋ฉด์ ์คํฐ๋ ์งํ ์ค!
๋ฉด์ ์คํฐ๋๋ฅผ ์งํํ๋ฉด์ ์ด๋ก ์ ์ธ ๋ด์ฉ์ ๋ํด ๊ณต๋ถํ๊ธฐ ์์ํ๋ค. ์์ฑ์ผ๋ก ์งํํ๊ณ ์์ด์ ๊ฐ๋ ๋ค์ ๋ํด ๊น์ด ์๊ฒ ๊ณต๋ถํ์ง ๋ชปํ๊ณ ์์ด์ ๊ฑฑ์ ์ด๋ค. ๋ฉด์ ์ค๋น๋ผ๊ณ ํด์ ๋๋ต์ฉ์ผ๋ก๋ง ์ค๋นํ๋๊ฒ ์๋๋ผ ๋๋ตํ๋ ค๋ฉด ๋ ๊น์ด ์๋ ๊ณต๋ถ๊ฐ ํ์ํ๋ค๊ณ ์๊ฐํ๋ค. ํ๋ฒ ๋น ๋ฅด๊ฒ ํ๊ณ ๋๋ฉด ํ๋์ฉ ๋ ๊น์ด ์๊ฒ ๊ณต๋ถํด๋ณด๊ณ ์ถ๋ค.

'์ผ์ > Today I Learned' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2023.04.21[์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ, ๋ฉด์ ์คํฐ๋] (0) | 2023.04.21 |
---|---|
2023.04.20[DFS] (0) | 2023.04.20 |
2023.04.14[๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ] (0) | 2023.04.14 |
2023.04.12[๋น์ ์ด ์ปดํฌ๋ํธ, ํ๋ก์ ํธ ๋ฆฌํฉํ ๋ง] (0) | 2023.04.12 |
2023.04.03[์ ํด๋ฆฌ๋ ํธ์ ๋ฒ, ํต ์ ๋ ฌ] (0) | 2023.04.04 |
๐์ค๋ ๋ฐฐ์ด ๊ฒ
๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ตฌํ
class Node{
constructor(val){
this.val = val;
this.next = null;
}
}
class SinglyLinkedList{
constructor(){
this.head = null;
this.tail = null;
this.length = 0;
}
push(val){
const newNode = new Node(val);
if(!this.head){
this.head = newNode;
this.tail = this.head;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.length++;
return this;
}
pop(){
if(!this.head) return undefined;
let current = this.head;
let newTail = current;
while(current.next){
newTail = current;
current = current.next;
}
this.tail = newTail;
this.tail.next = null;
this.length--;
if(this.length === 0){
this.head = null;
this.tail = null;
}
return current;
}
shift(){
if(!this.head) return undefined;
const currentHead = this.head;
this.head = currentHead.next;
this.length--;
if(this.length === 0){
this.tail = null;
}
return currentHead;
}
unshift(val){
const newNode = new Node(val);
if(!this.head) {
this.head = newNode;
this.tail = this.head;
}
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
get(index){
if(index < 0 || index >= this.length) return null;
let counter = 0;
let current = this.head;
while(counter !== index){
current = current.next;
counter++;
}
return current;
}
set(index, val){
let foundNode = this.get(index);
if(foundNode){
foundNode.val = val;
return true;
}
return false;
}
insert(index, val){
if(index < 0 || index > this.length) return false;
if(index === this.length) return !!this.push(val);
if(index === 0) return !!this.unshift(val);
const newNode = new Node(val);
const prev = this.get(index - 1);
const temp = prev.next;
prev.next = newNode;
newNode.next = temp;
this.length++;
return true;
}
remove(index){
if(index < 0 || index >= this.length) return undefined;
if(index === 0) return this.shift();
if(index === this.length - 1) return this.pop();
const previousNode = this.get(index - 1);
const removed = previousNode.next;
previousNode.next = removed.next;
this.length--;
return removed;
}
reverse(){
let node = this.head;
this.head = this.tail;
this.tail = node;
let next;
let prev = null;
for(var i = 0; i < this.length; i++){
next = node.next;
node.next = prev;
prev = node;
node = next;
}
return this;
}
print(){
const arr = [];
let current = this.head
while(current){
arr.push(current.val)
current = current.next
}
console.log(arr);
}
}
๋ฉด์ ์คํฐ๋ ์งํ ์ค!
๋ฉด์ ์คํฐ๋๋ฅผ ์งํํ๋ฉด์ ์ด๋ก ์ ์ธ ๋ด์ฉ์ ๋ํด ๊ณต๋ถํ๊ธฐ ์์ํ๋ค. ์์ฑ์ผ๋ก ์งํํ๊ณ ์์ด์ ๊ฐ๋ ๋ค์ ๋ํด ๊น์ด ์๊ฒ ๊ณต๋ถํ์ง ๋ชปํ๊ณ ์์ด์ ๊ฑฑ์ ์ด๋ค. ๋ฉด์ ์ค๋น๋ผ๊ณ ํด์ ๋๋ต์ฉ์ผ๋ก๋ง ์ค๋นํ๋๊ฒ ์๋๋ผ ๋๋ตํ๋ ค๋ฉด ๋ ๊น์ด ์๋ ๊ณต๋ถ๊ฐ ํ์ํ๋ค๊ณ ์๊ฐํ๋ค. ํ๋ฒ ๋น ๋ฅด๊ฒ ํ๊ณ ๋๋ฉด ํ๋์ฉ ๋ ๊น์ด ์๊ฒ ๊ณต๋ถํด๋ณด๊ณ ์ถ๋ค.

'์ผ์ > Today I Learned' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2023.04.21[์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ, ๋ฉด์ ์คํฐ๋] (0) | 2023.04.21 |
---|---|
2023.04.20[DFS] (0) | 2023.04.20 |
2023.04.14[๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ] (0) | 2023.04.14 |
2023.04.12[๋น์ ์ด ์ปดํฌ๋ํธ, ํ๋ก์ ํธ ๋ฆฌํฉํ ๋ง] (0) | 2023.04.12 |
2023.04.03[์ ํด๋ฆฌ๋ ํธ์ ๋ฒ, ํต ์ ๋ ฌ] (0) | 2023.04.04 |