LeeCode 148 Sort List

本博客介绍了一种将链表中的元素取出、排序后再构建一条新链表的方法,详细步骤包括创建辅助vector、排序及重构链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目










分析


全部取出来放入vector中进行排序再构造一条新链表





题解


/**
 * 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) {
        vector<int> ans;
        if(head == NULL)
            return NULL;
        while(head != NULL){
            ans.push_back(head->val);
            head = head->next;
        }
        sort(ans.begin(),ans.end());
        ListNode newhead(0);
        ListNode *temphead = &newhead;
        ListNode *p = &newhead;
        for(int i=0;i<ans.size();i++){
            ListNode *temp = new ListNode(ans[i]);
            temphead->next = temp;
            temphead = temphead->next;
        }
        p = p->next;
        return p;
    }
};





### LeetCode Java 面试题 以下是基于提供的引用内容以及专业知识整理的一些常见的 LeetCode Java 面试题: #### 一:链表反转 实现单链表的反转是一个经典的算法题目。可以通过迭代或者递归来完成此操作[^3]。 ```java public ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev; } ``` #### 二:计 N 以内的素数 统计小于非负整数 `n` 的质数数量可以使用埃拉托斯特尼筛法来高效解决。 ```java public int countPrimes(int n) { boolean[] isPrime = new boolean[n]; Arrays.fill(isPrime, true); for (int i = 2; i * i < n; i++) { if (!isPrime[i]) continue; for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } int count = 0; for (int i = 2; i < n; i++) { if (isPrime[i]) count++; } return count; } ``` #### 三:删除排序数组中的重复项 给定一个升序排列的数组,移除其中的重复元素并返回新长度。 ```java public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int uniqueIndex = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[uniqueIndex]) { uniqueIndex++; nums[uniqueIndex] = nums[i]; } } return uniqueIndex + 1; } ``` #### 四:寻找数组的中心下标 找到数组的一个索引位置使得其左侧和右侧的总和相等。 ```java public int pivotIndex(int[] nums) { int totalSum = 0; for (int num : nums) totalSum += num; int leftSum = 0; for (int i = 0; i < nums.length; ++i) { if (leftSum == (totalSum - leftSum - nums[i])) return i; leftSum += nums[i]; } return -1; } ``` #### 五:x 的平方根 计算某个正数的平方根而不使用库函数。 ```java public int mySqrt(int x) { long start = 0, end = x; while(start <= end){ long mid = start + (end - start)/2; if(mid*mid == x) return (int)mid; else if(mid*mid > x) end = mid-1; else start = mid+1; } return (int)end; } ``` #### 六:数组中三个数的最大乘积 找出数组中任意三个数能够组成的最大乘积。 ```java public int maximumProduct(int[] nums) { Arrays.sort(nums); int option1 = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]; int option2 = nums[0] * nums[1] * nums[nums.length - 1]; return Math.max(option1, option2); } ``` #### 七:两数之和 在一个数组中查找两个加起来等于目标值的数,并返回它们的索引。 ```java public int[] twoSum(int[] numbers, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < numbers.length; i++) { int complement = target - numbers[i]; if (map.containsKey(complement)) { return new int[]{map.get(complement), i}; } map.put(numbers[i], i); } throw new IllegalArgumentException("No two sum solution"); } ``` #### 八:斐波那契数列 通过动态规划方法优化时间复杂度为 O(n)。 ```java public int fib(int N) { if(N<=1) return N; int a=0,b=1,c=0; for(int i=2;i<=N;i++){ c=a+b; a=b; b=c; } return b; } ``` #### 九:乐观锁与悲观锁的理解及其实现方式 乐观锁通常依赖数据库版本号机制或时间戳控制并发访问冲突;而悲观锁则利用数据库事务隔离级别(如 SELECT FOR UPDATE)锁定记录防止修改[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值