Leedcode算法专题训练(链表)
1.发现两个链表的交点
160.两个链表的交集(容易)
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA=headA;
ListNode curB=headB;
while(curA!=curB){
if(curA==null){
curA=headB;
}else{
curA=curA.next;
}
if(curB==null){
curB=headA;
}
else{
curB=curB.next;
}
}
return curA;
}
}
2. 链表反转
206. Reverse Linked List (Easy)
方法一:迭代
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode next=null;
while(head!=null){
next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}
}
方法二:递归
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode next= head.next;
ListNode cur=reverseList(next);
next.next=head;
head.next=null;
return cur;
}
}
3. 归并两个有序的链表
21. Merge Two Sorted Lists (Easy)
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head=new ListNode(0);
ListNode cur=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
cur.next=l1;
l1=l1.next;
}
else{
cur.next=l2;
l2=l2.next;
}
cur=cur.next;
}
cur.next=l1!=null?l1:l2;
return head.next;
}
}
4. 从有序链表中删除重复节点
83. Remove Duplicates from Sorted List (Easy)
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur=head;
while(cur!=null&&cur.next!=null){
if(cur.next.val==cur.val){
cur.next=cur.next.next;
}
else cur=cur.next;
}
return head;
}
}
5. 删除链表的倒数第 n 个节点
19. Remove Nth Node From End of List (Medium)
这个题目一次遍历的方法挺好的,就是双指针的方式
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode first =head;
while(n-->0){
first=first.next;
}
if(first==null)return head.next;
ListNode second =head;
while(first.next!=null){
first=first.next;
second=second.next;
}
second.next=second.next.next;
return head;
}
}
6. 交换链表中的相邻结点
24. Swap Nodes in Pairs (Medium)
Given 1->2->3->4, you should return the list as 2->1->4->3.
这题目没做出来需要好好思考,两个指针是可以进行互换的但是需要有个temp节点来记录前一个节点
非递归的解法
class Solution {
public ListNode swapPairs(ListNode head) {
//终止条件:链表只剩一个节点或者没节点了,没得交换了。返回的是已经处理好的链表
if(head == null || head.next == null){
return head;
}
//一共三个节点:head, next, swapPairs(next.next)
//下面的任务便是交换这3个节点中的前两个节点
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
//根据第二步:返回给上一级的是当前已经完成交换后,即处理好了的链表部分
return next;
}
}
递归解法,需要重视的是递归解法需要重视单个递归函数做了什么, 而不是下一层函数做了什么,因此我们只需要关注一级递归的解决过程即可。
因此,也就有了我们解递归题的三部曲:
- 找整个递归的终止条件:递归应该在什么时候结束?
- 找返回值:应该给上一级返回什么信息?
- 本级递归应该做什么:在这一级递归中,应该完成什么任务?

7. 链表求和
445. Add Two Numbers II (Medium)
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
题目要求:不能修改原始链表。
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
while(l1!=null){
stack1.push(l1.val);
l1=l1.next;
}
while(l2!=null){
stack2.push(l2.val);
l2=l2.next;
}
int carry=0;
ListNode head=null;
while(!stack1.isEmpty()||!stack2.isEmpty()||carry!=0){
int a=stack1.isEmpty()?0:stack1.pop();
int b=stack2.isEmpty()?0:stack2.pop();
int cur=a+b+carry;
carry=cur/10;
cur%=10;
ListNode curnode =new ListNode(cur);
curnode.next=head;
head=curnode;
}
return head;
}
}
8. 回文链表
234. Palindrome Linked List (Easy)
题目要求:以 O(1) 的空间复杂度来求解。
切成两半,把后半段反转,然后比较两半是否相等。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode cur=head;
ArrayList<Integer> arr=new ArrayList<>();
while(cur!=null){
arr.add(cur.val);
cur=cur.next;
}
int count=arr.size();
for(int i=0;i<count;i++){
if(!arr.get(i).equals(arr.get(count-i-1)))return false;
}
return true;
}
}
get()方法对127以内的整形数值会直接封装成值返回,但是对大于127的值会返回对象,因此你的比较是进行对象地址的比较,实际上是不对的。你得用equals()方法比较。
class Solution {
ListNode temp;
public boolean isPalindrome(ListNode head) {
temp=head;
return check(head);
}
private boolean check(ListNode head){
if(head==null){
return true;
}
boolean res=check(head.next)&&(temp.val==head.val);
temp=temp.next;
return res;
}
}
这个递归太帅了,太有技巧性了
9. 分隔链表
725. Split Linked List in Parts(Medium)
Input:
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
题目描述:把链表分隔成 k 部分,每部分的长度都应该尽可能相同,排在前面的长度应该大于等于后面的。
class Solution {
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur=root;
int count=0;
while(cur!=null){
count++;
cur=cur.next;
}
int m=count/k;
int n=count%k;
ListNode[] arr=new ListNode[k];
cur=root;
for(int i=0;i<k&&cur!=null;i++){
arr[i]=cur;
int cursize=m+(n-->0?1:0);
for(int j=0;j<cursize-1;j++){
cur=cur.next;
}
ListNode next=cur.next;
cur.next=null;
cur=next;
}
return arr;
}
}
10. 链表元素按奇偶聚集
328. Odd Even Linked List (Medium)
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head==null){
return head;
}
ListNode odd=head, even =head.next,evenhead=even;
while(even!=null&&even.next!=null){
odd.next=odd.next.next;
odd=odd.next;
even.next=even.next.next;
even=even.next;
}
odd.next=evenhead;
return head;
}
}
Leedcode算法专题训练(链表)的更多相关文章
- Leedcode算法专题训练(双指针)
算法思想 双指针 167. 两数之和 II - 输入有序数组 双指针的典型用法 如果两个指针指向元素的和 sum == target,那么得到要求的结果: 如果 sum > target,移动较 ...
- Leedcode算法专题训练(树)
递归 一棵树要么是空树,要么有两个指针,每个指针指向一棵树.树是一种递归结构,很多树的问题可以使用递归来处理. 1. 树的高度 104. Maximum Depth of Binary Tree (E ...
- Leedcode算法专题训练(搜索)
BFS 广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点.需要注意的是,遍历过的节点不能再次被遍历. 第一层: 0 -> {6,2,1,5} ...
- Leedcode算法专题训练(分治法)
归并排序就是一个用分治法的经典例子,这里我用它来举例描述一下上面的步骤: 1.归并排序首先把原问题拆分成2个规模更小的子问题. 2.递归地求解子问题,当子问题规模足够小时,可以一下子解决它.在这个例子 ...
- Leedcode算法专题训练(二分查找)
二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...
- Leedcode算法专题训练(排序)
排序 快速排序 用于求解 Kth Element 问题,也就是第 K 个元素的问题. 可以使用快速排序的 partition() 进行实现.需要先打乱数组,否则最坏情况下时间复杂度为 O(N2). 堆 ...
- Leedcode算法专题训练(贪心)
1. 分配饼干 455. 分发饼干 题目描述:每个孩子都有一个满足度 grid,每个饼干都有一个大小 size,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足.求解最多可以获得满足的孩子数 ...
- Leedcode算法专题训练(位运算)
https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...
- Leedcode算法专题训练(数组与矩阵)
1. 把数组中的 0 移到末尾 283. Move Zeroes (Easy) Leetcode / 力扣 class Solution { public void moveZeroes(int[] ...
随机推荐
- RocketMq灰皮书(一)------选型&RocketMQ名词
RocketMq灰皮书(一)------选型&RocketMQ名词 一. MQ选型对比 目前业内常用的MQ框架有一下几种: Kafka RabbitMQ RocketMQ 除此之外,还有Act ...
- mysql一张表到底能存多少数据?
前言 程序员平时和mysql打交道一定不少,可以说每天都有接触到,但是mysql一张表到底能存多少数据呢?计算根据是什么呢?接下来咱们逐一探讨 知识准备 数据页 在操作系统中,我们知道为了跟磁盘交互, ...
- 深入理解 Web 协议 (三):HTTP 2
本篇将详细介绍 HTTP 2 协议的方方面面,知识点如下: HTTP 2 连接的建立 HTTP 2 中帧和流的关系 HTTP 2 中流量节省的奥秘:HPACK 算法 HTTP 2 协议中 Server ...
- ELK的一点认识
为什么需要ELK: 一般大型系统是一个分布式部署的架构,不同的服务模块部署在不同的服务器上,问题出现时,大部分情况需要根据问题暴露的关键信息,定位到具体的服务器和服务模块,构建一套集中式日志系统,可以 ...
- 前端传递数据到后台的两种方式;创建一个map或者创建一个FormData对象
一.构建一个map getAllDeptAllUsers(){ const modleCode = {'auditMenuId': this.auditMenuId, 'enterpriseId': ...
- python模块win32com中的early-bind与lazy-bind(以Autocad为例)
1.什么是Lazy-bind模式,Early-bind模式? win32com中,Lazy-bind 模式指的是程序事先不知道对象的任何方法和属性,当对象属性,方法被调用时,程序才向对象发出一个询问( ...
- 多Excel文件内容查询工具。
多Excel文件内容查询工具. 告别繁琐重复的体力劳动,一分钟干完一天的活. 码云 github 下载 当需要在多个Excel表格中查询需要的信息是,一个文件一个文件的去查询非常麻烦. 虽然有其他方法 ...
- 区分函数防抖&函数节流
1. 概念区分 函数防抖:触发事件后,在n秒内函数只能执行一次,如果触发事件后在n秒内又触发了事件,则会重新计算函数延执行时间. 简单说: 频繁触发, 但只在特定的时间内才执行一次代码,如果特定时间内 ...
- 第十届蓝桥杯省赛-试题E: RSA 解密
试题E: RSA 解密 这里涉及到很多数论的知识:质因子分解,扩展欧几里得算法,快速幂算法,利用快速乘算法求解快速幂(mod太大导致不能直接乘,而是需要使用加法来替代乘法) 另外还需要注意扩展欧几里得 ...
- FreeBSD 开发已经迁移至 git
FreeBSD 开发已经迁移至 git 全部预计于 2021 年 3 月完成迁移. https://git.freebsd.org/src.git 或者 https://cgit.freebsd.or ...