`
michelle_0916
  • 浏览: 394 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

leetcode: sort list

阅读更多

Sort a linked list in O(n log n) time using constant space complexity.

====analysis=======

mergeSort for singly-linked list 

====code=======

 
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeSort(ListNode *head, ListNode *newhead) {
if(!head) return newhead;
if(!newhead) return head;

ListNode *smaller = head->val <= newhead->val ? head : newhead;
ListNode *larger = head->val > newhead->val ? head : newhead;
ListNode *result = smaller;

while(smaller->next && larger) {
if(smaller->val <= larger->val && larger->val < smaller->next->val) {

ListNode *q = larger->next;
larger->next = smaller->next;
smaller->next = larger;

smaller = smaller->next;
larger = q;
} else {
smaller = smaller->next;
}
}

if(larger) {
smaller->next = larger;
}

return result;
}
ListNode *sortList(ListNode *head) {
if(!head || !head->next) return head;
ListNode *slow = head;
ListNode *fast = head->next;
while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}

ListNode *rightP = slow->next;
slow->next = NULL;
head = sortList(head);
rightP = sortList(rightP);

return mergeSort(head, rightP);
}
};

 

分享到:
评论

相关推荐

    javalruleetcode-LeetCode:LeetCode算法问题

    Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest ...

    leetcode分类-leetcode:leetcode

    list:链表相关题目 stack:栈相关题目 queue:队列相关题目 string:字符串处理相关题目 tree:树相关题目 divide Conquer: 分治法相关题目 dynamic Programming:动态规划 graph:图论 greedy:贪心算法 recursion:...

    leetcode答案-leetcode:leetcode

    leetcode 答案 leetcode Day 1 两数之和: 1。 考虑两层嵌套循环 2。...用dictionary以及 ...List[int], ...List[int]: ...List[int], ...List[int]: ...temp.sort() i=0 j=len(nums)-1 while i&lt;j&gt;target: j=j-1 elif (temp

    最大公共字符串leetcode-leetCode:leetcode

    最大公共字符串leetcode leetcode 数组 链表 二叉树 位操作 判断字符串的顺序排列 给定一个字符串数组,将字谜组合在一起。 例如,给定:["eat", "tea", "tan", "ate", "nat", "bat"], public class Solution { ...

    leetcode卡-LeetCode:我的LeetCode解决方案

    list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. Trapping Rain Water II], BFS/...

    蓄水池算法leetcode-leetcode:Python中leetcode问题的解决方法

    Sort & Search # Name Difficulty Solution index 1 直接插入 easy python :heart_suit: 2 简单选择排序 easy python :heart_suit: 3 冒泡排序 easy python :heart_suit: 4 希尔 easy python :heart_suit: 5 快排...

    leetcode中325题python-leetcode:leetcode

    leetcode中325题python leetcode 以 参考 和 Hash相关 1_两数之和 387_字符串中的第一个唯一字符 链表操作 2 ...删除链表的倒数第N个节点 ...sort-list 234 回文链表 palindrome-linked-list 双指针遍历/滑动

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    lrucacheleetcode-leetcode:leetcode

    frequecy_sort.py: height_checker.py: Jewels_and_stones.py: last_stone_weight.py: Linked_list_cycle.py: long_pressed_name.py: max_69_number.py: max_array_sum_after_negations.py: max_depth_n_ary...

    刷leetcode不用stl-leetcode:leetcode

    list unordered_map&lt;&gt; 88 有序数组 合并两个vector STL vector array 26 就地删除重复元素 直接用STL sort() unique() erase()做的 122 买卖股票的最佳时机 利润最大化就是每次波谷买入波峰卖出 189 旋转数组 ...

    javalruleetcode-leetcode:leetcode

    java lru leetcode leetcode Description My leetcode solutions. Statistics language num C++ 308 Python3 46 JavaScript 4 Java 2 SQL ...Sort ...Sort ...Sort ...Topological-Sort-拓扑排序 ...List 双向链表

    leetcode下载-Leetcode:力码

    leetcode下载 Leetcode 刷题记录 01. Two Sum 暴力破解时间复杂度高,使用hash表降低时间复杂度 02. Add Two Numbers 由于链表是逆置的,所以直接顺序遍历两个链表,按照加法器规则依次相加各节点,并进位 最后一组...

    leetcode338-LeetCode:记录LeetCode题解

    LeetCode 解决方案。 不。 标题 笔记 标签 1 Array 2 Linked List 3 Hash Table String 4 Array Divide and Conquer Binary Search Divide and Conquer 7 Math 24 Linked List 27 Array 33 Array Binary Search 79 ...

    leetcode分类-leetCode:leetcode

    leetcode 分类 ReadMe 纯粹记录一下自己leetCode做题记录及部分思路笔记,不充当指导性repo 加油!...sort 循环排序 list 链表 ... 这些tag分类都可以在一些平台上找到,VSCode中也有响应的分类tag

    leetcode中国-LeetCode:LeetCode经典题目分类汇总(JavaScript实现)

    List ) 栈 ( Stack ) 树 ( Tree ) 二叉搜索树 ( BST ) 字典树(前缀树) ( Trie ) 哈希表 ( Hash Table ) 堆 ( Heap ) 图 ( Graph ) 二分查找 ( Binary Search ) 位运算 ( Bit Manipulation ) 分治算法 ( Divide And...

    扩展矩阵leetcode-leetcode:Leetnode笔记

    list.sort( key = lambda x: x[1] ) 图 = collections.defaultdict(list) heapq.heapify (列表) a = heapq.pop(队列) heapq.push(队列,ele) dq = collections.deque(list) append, appendleft, clear, copy...

    leetcode2sumc-LeetCode:LeetCode个人题解

    leetcode 2 sum c LeetCode LeetCode 个人题解, 解法基于C++ Content Title Difficulty Solution ...sort ...List 20. Easy stack 21. Easy merge sort 22. Medium recursion、dfs 23. Hard heap sort

    leetcode不会-Leetcode:力码

    Collections.sort(List,Comparator) 对列表进行排序 反向链表(给定链表反向部分的长度) ListNode start = pre.next; ListNode then = start.next; //key part of reversing given length linkedlist for(int i =...

Global site tag (gtag.js) - Google Analytics