博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Insertion Sort List
阅读量:4660 次
发布时间:2019-06-09

本文共 4290 字,大约阅读时间需要 14 分钟。

class Solution {public:    ListNode *insertionSortList(ListNode *head) {        if (head == NULL) return NULL;        ListNode* sorted_head = head;        ListNode* unsorted_head = head->next;        head->next = NULL;                ListNode* cur = unsorted_head;        while (cur != NULL) {            unsorted_head = cur->next;            cur->next = NULL;            sorted_head = insertNode(sorted_head, cur);            cur = unsorted_head;        }                return sorted_head;    }        ListNode* insertNode(ListNode* list, ListNode* node) {        if (list == NULL && node == NULL) return NULL;        if (node == NULL) return list;        if (list == NULL) return node;        ListNode* cur = list;        ListNode* pre = NULL;        while (cur != NULL && cur->val < node->val) {            pre = cur;            cur = cur->next;        }        if (pre == NULL) {            node->next = list;            return node;        } else {            node->next = pre->next;            pre->next = node;            return list;        }    }};

感觉很简单,写起来又是这错那错

 

第二轮顺一些,先在纸上写好

Sort a linked list using insertion sort.

 

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode *insertionSortList(ListNode *head) {        if (head == NULL) {            return NULL;        }        ListNode* sorted = head;                ListNode* cur = head->next;        // must after last line        head->next = NULL;                while (cur != NULL) {            ListNode* next = cur->next;            cur->next = NULL;            sorted = insertNode(sorted, cur);            cur = next;        }        ListNode* pre = NULL;        return sorted;    }        ListNode *insertNode(ListNode* head, ListNode* node) {        if (head == NULL) {            return node;        }        if (node == NULL) {            return head;        }        ListNode* cur = head;        ListNode* pre = NULL;        while (cur != NULL && cur->val < node->val) {            pre = cur;            cur = cur->next;        }        node->next = cur;        if (pre == NULL) {            return node;        } else {            pre->next = node;            return head;        }    }};

 再来一发Python版本的,没有用python专用语法,比cpp慢了30倍

# Definition for singly-linked list.# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    # @param head, a ListNode    # @return a ListNode    def insertionSortList(self, head):        if head is None:            return None                cur = head.next        sorted = head        sorted.next = None                while cur is not None:            next = cur.next            cur.next = None            sorted = self.insertNode(sorted, cur)            cur = next        return sorted            def insertNode(self, head, node):        if head is None:            return node                if node is None:            return head                cur = head        pre = None                while (cur is not None and cur.val < node.val):            pre = cur            cur = cur.next                node.next = cur        if pre is None:            return node        else:            pre.next = node            return head

 再来:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* insertionSortList(ListNode* head) {        ListNode* list2 = NULL;                ListNode* curr = head;                while (curr != NULL) {            ListNode* next = curr->next;            list2 = insertion(list2, curr);               curr = next;        }        return list2;    }        ListNode* insertion(ListNode* list, ListNode* node) {        ListNode* head = list;        ListNode* prev = NULL;        while (list != NULL && node->val > list->val) {            prev = list;            list = list->next;        }        if (prev == NULL) {            node->next = list;            head = node;        } else {            node->next = prev->next;            prev->next = node;        }        return head;    }};

 

转载于:https://www.cnblogs.com/lailailai/p/3720755.html

你可能感兴趣的文章
关于jedis2.4以上版本的连接池配置,及工具类
查看>>
记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
查看>>
FactoryBean
查看>>
Coolite动态加载CheckboxGroup,无法在后台中获取
查看>>
C3P0连接池工具类使用
查看>>
SVN常用命令备注
查看>>
孩子教育
查看>>
解决Cacti监控图像断断续续问题
查看>>
结构体的传参理解成员的存储方式
查看>>
python 进程与线程(理论部分)
查看>>
什么是API
查看>>
强名称程序集(strong name assembly)——为程序集赋予强名称
查看>>
1028. List Sorting (25)
查看>>
BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
查看>>
ubuntu 重启命令,ubuntu 重启网卡方法
查看>>
Linux的学习:
查看>>
JavaScript中的原型继承原理
查看>>
Python logger模块
查看>>
jquery控制css的display(控制元素的显示与隐藏)
查看>>
关于python做人工智能的一个网页(很牛逼)
查看>>