leet code Sort List

*/-->

pre.src {background-color: Black; color: White;}

pre.src {background-color: Black; color: White;}

leet code Sort List

对链表使用快慢指针归并排序

Sort List

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

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (head == NULL || head->next == NULL) {
            return head;
        }
        ListNode *fast = head;
        ListNode *slow = head;
        while (fast->next != NULL && fast->next->next != NULL) {
            fast = fast->next->next;
            slow = slow->next;
        } // slow 指到中间,fast 直到了结尾
        fast = slow;          // 先保留 mid 的值
        slow = slow->next;    // 相当于 mid+1
        fast->next = NULL;    // 断掉链表

        ListNode *l1 = sortList(head);
        ListNode *l2 = sortList(slow);
        return merge(l1, l2);
    }

    ListNode *merge(ListNode *l1, ListNode *l2) {  // 合并链表
        ListNode t(-1);
        ListNode *p = &t;
        while (l1 != NULL && l2 != NULL) {
            if (l1->val > l2->val) {
                p->next = l2;
                l2 = l2->next;
                p = p->next;
            } else {
                p->next = l1;
                l1 = l1->next;
                p = p->next;
            }
        }
        while (l1 != NULL) {
            p->next = l1;
            l1 = l1->next;
            p = p->next;
        }
        while (l2 != NULL) {
            p->next = l2;
            l2 = l2->next;
            p = p->next;
        }
        return t.next;         // 注意返回
    }
};
时间: 2024-12-04 17:49:21

leet code Sort List的相关文章

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

#Leet Code# Unique Tree

语言:Python 描述:使用递归实现 1 class Solution: 2 # @return an integer 3 def numTrees(self, n): 4 if n == 0: 5 return 0 6 elif n == 1: 7 return 1 8 else: 9 part_1 = self.numTrees(n-1) * 2 10 part_2 = 0 11 12 for i in range(1,n-1): 13 part_left = self.numTrees(

#Leet Code# Sqrt

描述:log(n) 代码: 1 class Solution: 2 # @param x, an integer 3 # @return an integer 4 def getVal(self, begin, end, x): 5 if end == begin : 6 return begin 7 if end == begin + 1: 8 return begin 9 10 while True: 11 mid = (begin + end) / 2 12 tmp = mid * mid

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

#Leet Code# Gray Code

描述: 要求相邻数2进制差一位 先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者连接上了 代码: 1 class Solution: 2 # @return a list of integers 3 def grayCode(self, n): 4 if n == 0: return [0] 5 6 s1 = self.grayCode(n - 1) 7 s2 = [item

Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

#Leet Code# Same Tree

语言:Python 描述:使用递归实现 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 # @param p, a tree node 10 # @param q, a tree node 11 # @return

#Leet Code# LRU Cache

语言:C++ 描述:使用单链表实现,HeadNode是key=-1,value=-1,next=NULL的结点.距离HeadNode近的结点是使用频度最小的Node. 1 struct Node { 2 int key; 3 int value; 4 Node* next; 5 }; 6 7 class LRUCache { 8 public: 9 LRUCache(int capacity) { 10 this->capacity = capacity; 11 this->curLength