What about my accepted solution using java


  • 0
    S
        public class Solution {
    	public ListNode insertionSortList(ListNode head) {
    		if (head == null || head.next == null)
    			return head;
    
    		ListNode c = head.next;
    		head.next = null;
    		ListNode pt, h;
    
    		while (c != null) {
    
    			pt = c;
    			c = c.next;
    			pt.next = null;
    
    			if (head.val > pt.val) {
    				pt.next = head;
    				head = pt;
    				continue;
    			}
    
    			h = head;
    			while (h.next != null) {
    				if (h.next.val > pt.val) {
    					pt.next = h.next;
    					h.next = pt;
    					break;
    				}
    
    				h = h.next;
    			}
    
    			if (h.next == null) {
    				h.next = pt;
    			}
    		}
    
    		return head;
    	}
    }

  • 0
    C

    I am not sure you algorithm follows the concept of insertion sort.


Log in to reply
 

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