BJ-2164-카드2

이은지·2023년 1월 16일
0

코딩테스트

목록 보기
60/76

문제
https://www.acmicpc.net/problem/2164

풀이

const readline = require("readline");
const { arrayBuffer } = require("stream/consumers");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on("line", inputOn);

rl.on("close", () => {
  process.exit();
});

function inputOn(n) {
  const N = Number(n);

  class Node {
    constructor(val) {
      this.val = val;
      this.next = null;
      this.prev = null;
    }
  }

  class LinkedList {
    constructor() {
      this.head = null;
      this.tail = null;
      this.length = 0;
    }

    push(val) {
      const newNode = new Node(val);

      if (!this.head) {
        this.head = newNode;
      } else {
        this.tail.next = newNode;
        newNode.prev = this.tail;
      }

      this.tail = newNode;
      this.length++;

      return newNode;
    }

    getHead() {
      return this.head.val;
    }

    removeHead() {
      this.head = this.head.next;
      this.head.prev = null;
      this.length--;
    }

    getLength() {
      return this.length;
    }
  }

  const cards = new LinkedList();

  for (let i = 1; i <= N; i++) {
    cards.push(i);
  }

  while (cards.getLength() !== 1) {
    cards.removeHead();
    cards.push(cards.getHead());
    cards.removeHead();
  }
  console.log(cards.getHead());
}

마무리

다른사람 풀이봐도 이해아직 못함..ㅠㅠ
너무 어려운 문제..

0개의 댓글