Header Ad

Leetcode Insertion Sort List problem solution

In this Leetcode Insertion Sort List problem solution, we have given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head.

The steps of the insertion sort algorithm:

  1. Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
  3. It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in place into the sorted list with each iteration.

Leetcode LRU Cache problem solution


Problem solution in Python.

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        if head==None:
            return head
        h=head
        prev=head
        head=head.next
        while(head):
            node=h
            while(node!=head):
                if node.val>head.val and node==h:
                    prev.next=head.next
                    head.next=node
                    h=head
                    head=prev.next
                    break
                elif node.val>head.val:
                    prev.next=head.next
                    head.next=node
                    previous.next=head
                    head=prev.next
                    break
                previous=node
                node=node.next
            if node==head:
                prev=head
                head=head.next
        return h



Problem solution in Java.

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode pre = new ListNode(-11);
        ListNode sorted = new ListNode(head.val);
        pre.next = sorted;
        head = head.next;
        while(head != null) {
            if(head.val < sorted.val) {
                ListNode tempPre = pre;
                while(pre.next != null && pre.next.val < head.val) {
                    pre = pre.next;
                }
                ListNode temp = pre.next;
                pre.next = new ListNode(head.val);
                pre.next.next = temp;
                pre = tempPre;
            } else {
                sorted.next = new ListNode(head.val);
                sorted = sorted.next;
            }
            head = head.next;
        }
        return pre.next;
    }

}


Problem solution in C++.

class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        ListNode dummy(0);
        
        while (head != nullptr) {
            ListNode *next_head = head->next;
            ListNode *it = &dummy;
            while (it->next && it->next->val < head->val) {
                it = it->next;
            }
            head->next = it->next;
            it->next = head;
            head = next_head;
        }
        
        return dummy.next;
    }
};


Problem solution in C.

struct ListNode* insertionSortList(struct ListNode* head) {
    struct ListNode *newhead = NULL;
	struct ListNode *temp = NULL;
    struct ListNode *cur = NULL;
    struct ListNode *p = NULL;
    if (head != NULL)
    {
        temp = head;
        newhead = head;
        if (head != NULL)
        {
            cur = head->next;
            p = head->next;
        }
    }
	while (p)
	{
		struct ListNode *q = newhead;
		if (newhead->val >= cur->val)
		{
			temp->next = p->next;
			p = p->next;
			cur->next = newhead;
			newhead = cur;
			cur = p;
		}
		else
		{
			if (temp->val < cur->val)
			{
				temp = cur;
				p = p->next;
				cur = p;
			}
			else
			{
				while (q->next != temp && q->val < cur->val && q->next->val < cur->val)
				{
					q = q->next;
				}
				temp->next = p->next;
				p = p->next;
				cur->next = q->next;
				q->next = cur;				
				cur = p;
			}
		}
	}
	return newhead;
}


Post a Comment

0 Comments