Splitting a linked list in javascript

I am trying to split a linked list, but cannot make it work when there are no elements that are less than the current value you are looking at. the idea is based on the value of val, elements less than the value of the parameter go to the left in the linked list, and elements that exceed the value of the parameter go to the right. I changed some conditions for adding linked lists to "greatThan" and "lessThan", but then it will stop working if the item is in the middle. What am I missing? Stuck on this quite a bit. the most important function here is the "partition" function, everything else is an assistant.

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

LinkedList.prototype.makeNode = function (val) {
  var node = {};
  node.val = val;
  node.next = null;
  return node;
};

LinkedList.prototype.partition = function (val) {
  var lesserThanVal = new LinkedList();
  var greaterThanVal = new LinkedList();
  var iterator = this.head;

  while (iterator) {
    if (iterator.val < val) {
      lesserThanVal.addToTail(iterator.val);
    } else if (iterator.val >= val) {
      greaterThanVal.addToTail(iterator.val);
    }
    iterator = iterator.next;
  }

  //now merge them.
  if (lesserThanVal.head === null) {
    console.log("LESSER IS NULL")
    return greaterThanVal;
  }
  if (greaterThanVal.head === null) {
    console.log("GREATER IS NULL")
    return lesserThanVal;
  } else {
    //merge
    var pointer = lesserThanVal.head;
    while (pointer.next) {
      pointer = pointer.next;
    }
    pointer.next = greaterThanVal.head;
    lesserThanVal.tail = greaterThanVal.tail;


    console.log("SHOULD BE 9", lesserThanVal.head.next.next);
    return lesserThanVal;
  }

};

LinkedList.prototype.addToTail = function (value) {

  var newTail = this.makeNode(value);

  if (!this.head) {
    this.head = newTail;
  }
  if (this.tail) {
    this.tail.next = newTail;
  }
  this.tail = newTail;
};

Tests:

    var list = new LinkedList();
    list.addToTail(8);
    list.addToTail(4);
    list.addToTail(5);
    list.addToTail(9);




 console.log(list);
    var partitionedList = list.partition(8); 
    returns { head: { val: 4, next: { val: 5, next: [8...] } },
      tail: { val: 9, next: null } }
    var partitionedList = list.partition(4); 
    returns { head: { val: 8, next: { val: 4, next: [5...] } },
      tail: { val: 9, next: null } }
    var partitionedList = list.partition(9); 
    returns { head: { val: 8, next: { val: 4, next: [{5...}] } },
      tail: { val: 9, next: null } }
    var partitionedList = list.partition(5);
    returns { head: { val: 4, next: { val: 8, next: [{5....}] } },
      tail: { val: 9, next: null } }
    console.log(partitionedList);

script: https://jsfiddle.net/e76vcwtp/

+4
2

. ) )

, , , .

, , , , - greatThanVal , , , lesserThanVal .

greaterThanVal.addToTail(val);
while (iterator) {
    if (iterator.val < val) {
      lesserThanVal.addToTail(iterator.val);
    } else if (iterator.val > val) {
      greaterThanVal.addToTail(iterator.val);
    }
    iterator = iterator.next;
  }

  //now merge them.
  if (lesserThanVal.head === null) {   
    return greaterThanVal;
  } else {
    var pointer = lesserThanVal.tail;
    pointer.next = greaterThanVal.head;
    lesserThanVal.tail = greaterThanVal.tail;
    return lesserThanVal;
  }
}
+1

, , , , , , = , .

while(iterator){
    if(iterator.val <= val){
        lesserThanVal.addToTail(iterator.val);
    }else if(iterator.val > val){
        greaterThanVal.addToTail(iterator.val);
    }
    iterator = iterator.next;
}
+2

Source: https://habr.com/ru/post/1615929/


All Articles