Share My Solution


  • 0
    O
    class Solution {
    public:
    	ListNode* insertionSortList(ListNode* head) {
    		ListNode* dummyHead = new ListNode(0);
    		ListNode *cur = head;
    		dummyHead->next	= head;
    		while (cur && cur->next) {
    			ListNode *next = cur->next, *pre = dummyHead;
    			while (pre != cur) {
    				if (pre->next->val > cur->next->val) {
    					cur->next = next->next;
    					next->next = pre->next;
    					pre->next = next;
    					break;
    				}
    				pre = pre->next;
    			}
    			if (pre == cur)
    			    cur = cur->next;
    		}
    		return dummyHead->next;
    	}
    };

Log in to reply
 

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