[help]inserting things in linked list


  • 0
    K
      var partition = function(head, x) {
    
    
    if(head === null) return null
    
    var headHead = new ListNode(0)
    
    headHead.next = head
    
    var before = headHead
    var p = before.next
    
    while(p!== null && p.val < x){
        before = p
        p = p.next
    }
    
    if(p === null || p.next === null) return head
    
    var pre = p
    
    var runner = p.next
    
    var pos = runner.next
    
    while( pos !== null){
    
      if(runner.val < x){
        //put runner in front of p
        before.next = runner
        runner.next = p
        pre.next = pos
      }
      
      pre = pre.next
      runner = pre.next
      pos = runner.next
    }
    return headHead.next
     }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.