JS solution


  • 0
    Y
    // https://leetcode.com/problems/insertion-sort-list/
    function insertionSortList(head) {
      if (head === null) {
        return head
      }
      let node = head.next
      while (node) {
        let prev = head
        while (prev !== node && prev.val <= node.val) {
          prev = prev.next
        }
        if (prev === node) {
          // everyting remains sorted in this pass
          node = node.next
          continue
        }
        let temp1 = node.val
        let temp2 = null
        while (prev) {
          if (temp1 === null) {
            temp1 = prev.val
            prev.val = temp2
            temp2 = null
          } else if (temp2 === null) {
            temp2 = prev.val
            prev.val = temp1
            temp1 = null
          }
          if (prev === node) {
            break
          }
          prev = prev.next
        }
        node = node.next
      }
      return head
    }
    

Log in to reply
 

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