Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const input =
- [
- '10 5',
- '10 2 1 6 8'
- ]
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- class NodeList {
- constructor(val) {
- this.val = val;
- this.next = null;
- this.prev = null;
- }
- }
- class DLL {
- constructor() {
- this.tail;
- this.head;
- this.map = new Map();
- }
- createList(num) {
- for (let i = 1; i <= num; i++) {
- if (!this.head) {
- const newNode = new NodeList(i);
- this.head = newNode
- this.tail = newNode;
- this.map.set(i, newNode);
- } else {
- const newNode = new NodeList(i);
- newNode.prev = this.tail;
- this.tail.next = newNode;
- this.tail = newNode;
- this.map.set(i, newNode);
- }
- }
- }
- iterate() {
- let temp = this.head
- let arr = [];
- while (temp) {
- arr.push(temp.val)
- temp = temp.next;
- }
- return arr;
- }
- shuffle(numberToShuffle) {
- let insertAfter;
- if (numberToShuffle % 2 === 0) {
- insertAfter = numberToShuffle / 2;
- } else if (numberToShuffle % 2 != 0) {
- insertAfter = numberToShuffle * 2;
- if (insertAfter > this.map.size) {
- insertAfter = this.map.size;
- }
- }
- const nodeToRemove = this.map.get(numberToShuffle);
- if (nodeToRemove === this.head) {
- this.head = this.head.next;
- this.head.prev = null;
- // nodeToRemove.prev = null;
- } else if (nodeToRemove === this.tail) {
- this.tail = this.tail.prev;
- this.tail.next = null;
- } else {
- nodeToRemove.prev.next = nodeToRemove.next;
- nodeToRemove.next.prev = nodeToRemove.prev;
- // nodeToRemove.next = null;
- // nodeToRemove.prev = null;
- }
- const newNode = new NodeList(numberToShuffle);
- console.log(newNode);
- const nodeToInsertAfter = this.map.get(insertAfter);
- if (nodeToInsertAfter === this.tail) {
- this.tail.next = newNode
- this.tail = newNode;
- this.map.set(numberToShuffle, newNode);
- } else {
- newNode.prev = nodeToInsertAfter;
- newNode.next = nodeToInsertAfter.next;
- nodeToInsertAfter.next = newNode;
- this.map.set(numberToShuffle, newNode);
- }
- }
- }
- const N = gets().split(' ').map(Number)
- const M = gets().split(' ').map(Number)
- const list = new DLL();
- list.createList(N[0]);
- for (let i = 0; i < N[1]; i++) {
- list.shuffle(M[i]);
- }
- print(list.iterate().join(' '));
Advertisement
Add Comment
Please, Sign In to add comment