1.发现两个链表的交点

160.两个链表的交集(容易)

Leetcode /力扣

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)

Leetcode / 力扣

方法一:迭代

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)

Leetcode / 力扣

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)

Leetcode / 力扣

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)

Leetcode / 力扣

这个题目一次遍历的方法挺好的,就是双指针的方式

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)

Leetcode / 力扣

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;
}
}

递归解法,需要重视的是递归解法需要重视单个递归函数做了什么, 而不是下一层函数做了什么,因此我们只需要关注一级递归的解决过程即可。

因此,也就有了我们解递归题的三部曲:

  1. 找整个递归的终止条件:递归应该在什么时候结束?
  2. 找返回值:应该给上一级返回什么信息?
  3. 本级递归应该做什么:在这一级递归中,应该完成什么任务?

7. 链表求和

445. Add Two Numbers II (Medium)

Leetcode / 力扣

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)

Leetcode / 力扣

题目要求:以 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)

Leetcode / 力扣

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)

Leetcode / 力扣

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算法专题训练(链表)的更多相关文章

  1. Leedcode算法专题训练(双指针)

    算法思想 双指针 167. 两数之和 II - 输入有序数组 双指针的典型用法 如果两个指针指向元素的和 sum == target,那么得到要求的结果: 如果 sum > target,移动较 ...

  2. Leedcode算法专题训练(树)

    递归 一棵树要么是空树,要么有两个指针,每个指针指向一棵树.树是一种递归结构,很多树的问题可以使用递归来处理. 1. 树的高度 104. Maximum Depth of Binary Tree (E ...

  3. Leedcode算法专题训练(搜索)

    BFS 广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点.需要注意的是,遍历过的节点不能再次被遍历. 第一层: 0 -> {6,2,1,5} ...

  4. Leedcode算法专题训练(分治法)

    归并排序就是一个用分治法的经典例子,这里我用它来举例描述一下上面的步骤: 1.归并排序首先把原问题拆分成2个规模更小的子问题. 2.递归地求解子问题,当子问题规模足够小时,可以一下子解决它.在这个例子 ...

  5. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...

  6. Leedcode算法专题训练(排序)

    排序 快速排序 用于求解 Kth Element 问题,也就是第 K 个元素的问题. 可以使用快速排序的 partition() 进行实现.需要先打乱数组,否则最坏情况下时间复杂度为 O(N2). 堆 ...

  7. Leedcode算法专题训练(贪心)

    1. 分配饼干 455. 分发饼干 题目描述:每个孩子都有一个满足度 grid,每个饼干都有一个大小 size,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足.求解最多可以获得满足的孩子数 ...

  8. Leedcode算法专题训练(位运算)

    https://www.cnblogs.com/findbetterme/p/10787118.html 看这个就完事了 1. 统计两个数的二进制表示有多少位不同 461. Hamming Dista ...

  9. Leedcode算法专题训练(数组与矩阵)

    1. 把数组中的 0 移到末尾 283. Move Zeroes (Easy) Leetcode / 力扣 class Solution { public void moveZeroes(int[] ...

随机推荐

  1. 高倍币VAST了解一下,如何掀起算力挖矿新热潮?

    随着比特币.以太坊等主流数字货币的起起落落,市场对于数字货币交易似乎进入了冷却期.很多生态建设者开启了观望态度,机构以及巨鲸们也开始纷纷着手分散投资.就在此时,一个新的概念逐步露出头角,吸引了大众关注 ...

  2. 微信附近的人,用redis也能实现?(GEO)

    相信微信附近的人的功能大家都应该用过 我可以很随意的通过我自己的定位能看到我附近的人,并且能看到那个人距离我的距离,大家有没有思考过这个是怎么实现的? 作为一个程序猿任何问题应该都有一个思考的过程,而 ...

  3. Ubuntu 下安装Anaconda + 显卡驱动 + CUDA + CUDNN + 离线安装环境

    写来给自己备忘,并不是什么教程- .- 下载安装包 Anaconda:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 显卡驱动:https ...

  4. HBase ——Shell操作

    HBase --Shell操作 Q:你觉得HBase是什么? A:一种结构化的分布式数据存储系统,它基于列来存储数据. 基于HBase,可以实现以廉价PC机器集群存储海量数据的分布式数据库的解决方案. ...

  5. Java基础语法:基本数据类型

    Java是一种强类型语言,每个变量都必须声明其类型. Java的数据类型 分为两大类:基本类型(primitive type)和引用类型(reference type). Java的所有八种基本类型的 ...

  6. Java基础语法:注释

    书写注释是一个非常好的习惯. 注释并不会被执行,是给我们写代码的人看的. Java中的注释有三种: 单行注释(Line comment) 多行注释(Block comment) 文档注释(JavaDo ...

  7. for、while的循环套用和函数的递归

    一.集成开发工具Eclipse 1.1 下载 官网下载 https://www.eclipse.org/downloads/download.php?file=/technology/epp/down ...

  8. 剑指 Offer 04. 二维数组中的查找 (思维)

    剑指 Offer 04. 二维数组中的查找 题目链接 本题的解法是从矩阵的右上角开始寻找目标值. 根据矩阵的元素分布特性, 当目标值大于当前位置的值时将row行号++,因为此时目标值一定位于当前行的下 ...

  9. 面试官:不会sql优化?出门右转顺便带上门,谢谢

    导读 作为一个后端程序员,数据库这个东西是绕不开的,特别是写sql的能力,如果您参加过多次面试,那么一定会从面试复盘中发现面试官总是会考察到sql优化这个东西. 我在之前的多次面试中最常遇到的一个问题 ...

  10. 【odoo14】odoo 14 Development Cookbook【目录篇】

    网上已经有大佬翻译过odoo12并且在翻译odoo14了.各位着急的可以自行搜索下... 这本书是为了让自己从odoo12转odoo14学习.也是为了锻炼下自己... odoo 14 Developm ...