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[] ...
随机推荐
- 利用Metasploit 打入ThinkPHP内网...
出品|MS08067实验室(www.ms08067.com) 本文作者:dch(Ms08067实验室 SRSP TEAM小组成员) 一.利用Metasploit进行攻击的流程图 Metasploi ...
- python 相对路径和绝对路径的区别
一,Python中获得当前目录和上级目录 获取当前文件的路径: from os import path d = path.dirname(__file__) #返回当前文件所在的目录 # __file ...
- C++算法代码——关于马的问题
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1285 题目描述 小R参加一个派对,这个派队的参加者需要带上四匹颜色不同的马.小R目前 ...
- Python3网络爬虫-- 使用代理,轮换使用各种IP访问
# proxy_list 代理列表 run_times = 100000 for i in range(run_times): for item in proxy_list: proxies = { ...
- Python学习笔记_爬虫数据存储为xlsx格式的方法
import requests from bs4 import BeautifulSoup import openpyxl wb=openpyxl.Workbook() sheet=wb.active ...
- java中this和super的用法及区别
this 用法1:代表当前对象本身 用法2:方法形参和类成员变量重名,用this进行区别 class demo{ private int age = 10; public int getAge(int ...
- 【原创】Linux虚拟化KVM-Qemu分析(十)之virtio驱动
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...
- LG gram 双系统全指南
LG gram 双系统全指南 为了和同学联机玩帝国时代2,以及为了下学期的编程课,五年没用过 Windows 的我决定装 Ubuntu20.04 LTS / WIndows 10 双系统了. 我的 L ...
- ArrayList源码阅读(小白的java进阶)
ArrayList(线程不安全) ArrayList是一个其容量能够动态增长的动态数组 继承关系 构造方法 是符合collection父接口的规范的 //传0则设置为默认容量 public Array ...
- react第三方库
作者:慕课网链接:https://www.zhihu.com/question/59073695/answer/1071631250来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...