ใŠ™๏ธ

LL PS - train connection

notion image
ย 
notion image
ย 

ํ’€์–ด๋ณด๊ธฐ

function Train(number) {
    this.number = number;
    this.next = null;
}

function LinkedList() {
    this.head = null;
}

function answer(nums) {
    let ll = new LinkedList();

    for (let x of nums) {
        let current = new Train(x);
        // ๋งจ ์ฒ˜์Œ ๋…ธ๋“œ๋Š” head์— ๋„ฃ์–ด์ฃผ์–ด์•ผ ๋œ๋‹ค.
        if (ll.head == null) ll.head = current;
        // ์ด๋ฏธ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด prev๋„ ๋ญ”๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ๋‹ค์Œ์—๋‹ค ๋งค๋‹ฌ๊ณ  prev๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
        else prev.next = current;
        prev = current;
    }

    return ll;
}