티스토리 뷰

Web 프로그래밍/hackrank

linked list

데브업 2019. 8. 26. 23:20

 

var node = new DoublyLinkedListNode();

node.data = data;

if (head == null) {

return node;

} else if (data <= head.data) {

node.next = head;

head.prev = node;

return node;

} else {

var rest = sortedInsert(head.next, data);

head.next = rest;

rest.prev = head;

return head;

}

 

 

 

 

 

 

 

 

 

 

 

function insertNodeAtPosition(head, data, position) {

var curr = head;

var prev = null;

var node = new SinglyLinkedListNode();

node.data = data;

 

var i = 0;

while (i < position) {

prev = curr;

curr = curr.next;

if (curr == null) { break;}

i++;

}

node.next = curr;

prev.next = node;

return head;

}