四种语言刷算法之对链表进行插入排序

力扣147. 对链表进行插入排序

1、C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* insertionSortList(struct ListNode* head){
    struct ListNode* newHead = head;
    struct ListNode* p = newHead;
    struct ListNode* q = head->next;
    
    newHead->next = NULL;
    while(q!=NULL){
        struct ListNode* r = NULL;
        struct ListNode* temp = q;
        q = q->next;
        while(p!=NULL&&p->val<temp->val){
            r = p;
            p = p->next;
        }
        if(r==NULL){
            temp->next = newHead;
            newHead = temp;
        }
        else{
            r->next = temp;
            temp->next = p;
        }
        p = newHead;
    }
    return newHead;
}

2、C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        ListNode* newHead = head;
        ListNode* q = head->next;
        newHead->next = nullptr;
        while(q!=nullptr){
            ListNode* p = newHead;
            ListNode* temp = q;
            ListNode* r = newHead;
            q = q->next;
            while(p!=nullptr&&temp->val>p->val){
                r = p;
                p = p->next;
            }
            if(r==p){
                temp->next = newHead;
                newHead = temp;
            }
            else{
                r->next = temp;
                temp->next = p;
            }
        }
        return newHead;
    }
};

3、JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode insertionSortList(ListNode head) {
        ListNode newHead = head;
        ListNode q = head.next;
        newHead.next = null;
        while(q!=null){
            ListNode p = newHead;
            ListNode r = newHead;
            ListNode temp = q;
            q = q.next;
            while(p!=null&&p.val<temp.val){
                r = p;
                p = p.next;
            }
            if(r==p){
                temp.next = newHead;
                newHead = temp;
            }
            else{
                r.next = temp;
                temp.next = p;
            }
        }
        return newHead;
    }
}

4、Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def insertionSortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        newHead = head
        q = head.next
        newHead.next = None
        while(q is not None):
            p = newHead
            r = newHead
            temp = q
            q = q.next
            while(p is not None and p.val<temp.val):
                r = p
                p = p.next
            if(r==p):
                temp.next = newHead
                newHead = temp
            else:
                r.next = temp
                temp.next = p
        return newHead
posted @ 2023-04-20 14:49  菜鸟冲冲冲  阅读(36)  评论(0)    收藏  举报