老师说过链表是分水岭,至少得会这些题目
目录
- 1.[删除链表中等于给定值 val 的所有节点](https://leetcode-cn.com/problems/remove-linked-list-elements/description/)
- 2.[反转一个单链表](https://leetcode-cn.com/problems/reverse-linked-list/description/).
- 3.[链表的中间结点 ](https://leetcode-cn.com/problems/middle-of-the-linked-list/description/)
- 4.[链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)
- 5.[合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/description/)
- 6.[删除链表中重复节点](https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef?tpId=13&&tqId=11209&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking)
- 7.[链表的回文结构](https://leetcode-cn.com/problems/palindrome-linked-list/)
- 8.[分割链表](https://leetcode-cn.com/problems/partition-list/)
在记录作者自己学习过程和解题时的思路方便复习的同时分享给其他同学们,如有错误或不足还请指正。
1.删除链表中等于给定值 val 的所有节点
public ListNode removeElements(ListNode head, int val) {ListNode newList = new ListNode();ListNode newCur = newList;ListNode cur = head;while (cur != null){if (cur.val != val){newCur.next = cur;newCur = cur;cur = cur.next;}else {cur = cur.next;}}newCur.next = null;return newList.next;}
public ListNode removeElements(ListNode head, int val) {ListNode newList = new ListNode();newList.next = head;ListNode prev = newList;ListNode cur = head;while (cur != null){if (cur.val == val){cur = cur.next;prev.next = cur;}else {prev = cur;cur = cur.next;}}return newList.next;}
public ListNode removeElements(ListNode head, int val){if (head == null){return null;}head.next = solve(head.next, val);return head.val == val ? head.next : head;}
2.反转一个单链表.
public ListNode reverseList(ListNode head) {if (head == null){return null;}Stack<ListNode> reversse = new Stack<>();ListNode cur = head;while(cur.next != null){reversse.push(cur);cur = cur.next;}ListNode newHead = cur;while (!reversse.isEmpty()){cur.next = reversse.pop();cur = cur.next;}cur.next = null;return newHead;}
public ListNode reverseList(ListNode head){ListNode prev = null;ListNode cur = head;while (cur != null){ListNode next = cur.next;cur.next = prev;prev = cur;cur = next;}return prev;}
public ListNode reverseList(ListNode head){if (head == null || head.next == null){return head;}//自递归出口返回head后,newhead一直没有变,因为newHead是//反转后链表的头结点ListNode newHead = reverseList(head);head.next.next = head;head.next = null;return newHead;}
3.链表的中间结点
解法:快慢指针发,定义两个指针指向头结点front,last,同时让两个指针往后走,fast一次走两步,slow一次往后走一步,当fast走完时,slow刚好走到中间位置。
public ListNode middleNode(ListNode head) {ListNode front = head;ListNode last = head;/*之所以循环结束有两个条件是因为如果有单数节点的话,front会停在最后一个结点处,再往后走就会出现空指针异常;如果有双数个节点的话,front最后会停在null(最后一个结点next指向null),此时慢的节点也刚好走到中间第二个节点。*/while (front != null && front.next != null){front = front.next.next;last = last.next;}return last;}
4.链表中倒数第k个节点
public ListNode FindKthToTail(ListNode head, int k) {//链表为空或者要找不存在的倒数第0个节点就返回空if (head == null || k == 0){return null;}ListNode fast = head;ListNode slow = head;int count = 1;while (fast.next != null){if (count < k){fast = fast.next;count++;}else {fast = fast.next;slow = slow.next;}}//因为存在k大于节点数的情况,即一共五个节点,但是要找倒数//第六个节点,这样的话在让头指针先走的过程中还没到第六个节点//就已经到null了退出循环了,此时count就会小于kif (count != k){return null;}return slow;
5.合并两个有序链表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {ListNode newList = new ListNode();ListNode cur1 = l1;ListNode cur2 = l2;ListNode cur = newList;while (cur1 != null && cur2 != null){if (cur1.val <= cur2.val){cur.next = cur1;cur = cur1;cur1 = cur1.next;}else {cur.next = cur2;cur = cur2;cur2 = cur2.next;}}if (cur1 == null){cur.next = cur2;}if (cur2 == null){cur.next = cur1;}return newList;}
6.删除链表中重复节点
public static ListNode deleteDuplication1(ListNode pHead) {if (pHead == null || pHead.next == null){return pHead;}ListNode frontNode = new ListNode(0);frontNode.next = pHead;ListNode prev = frontNode;ListNode fast = pHead.next;ListNode slow = pHead;while (fast != null){if (slow.val == fast.val){while (fast != null && fast.val == slow.val){fast = fast.next;}if (fast == null){prev.next = null;return frontNode.next;}else {slow = fast;fast = fast.next;prev.next = slow;}}else {prev = slow;slow = fast;fast = fast.next;}}return frontNode.next;}
7.链表的回文结构
public static boolean chkPalindrome(ListNode A) {StringBuilder str1 = new StringBuilder();StringBuilder str2 = new StringBuilder();Stack<ListNode> stack = new Stack<>();ListNode cur = A;while (cur != null){stack.push(cur);str1.append(cur.val);cur = cur.next;}while (!stack.isEmpty()){str2.append(stack.pop().val);}System.out.println(str1);System.out.println(str2);return str1.toString().equals(str2.toString());}
8.分割链表
public ListNode partition(ListNode head, int x) {if (head == null){return null;}ListNode small = new ListNode();ListNode smallCur = small;ListNode big = new ListNode();ListNode bigCur = big;ListNode cur = head;while (cur != null){if (cur.val < x){smallCur.next = cur;smallCur = cur;cur = cur.next;}else {bigCur.next = cur;bigCur = cur;cur = cur.next;}}bigCur.next = null;smallCur.next = big.next;return small.next;}
老师说过链表是分水岭,至少得会这些题目相关推荐
- 单表带换密码C语言程序,帮忙写个C语言程序!题目在下面!关于链表的。还没学,题目:“建立一个链表,逐个输入字符,每一个字符存放在一个链。...
帮忙写个C语言程序!题目在下面!关于链表的.还没学,题目:"建立一个链表,逐个输入字符,每一个字符存放在一个链. Posted By : Admin 2016-05-16 21:16 网友问 ...
- 尹成老师,关于链表的一些操作(C)
///.h文件 #include <stdio.h> #include <stdlib.h> struct student{ int num; float score; str ...
- 链表结构定义、操作以及经典题目
标题 前言 什么是链表(List/LinkedList) 链表的特点 单向链表的实现 凡是 结构操作 插入 删除 翻转 其他操作 单向链表代码 经典例题 单向循环链表 经典例题 双向链表实现(Doub ...
- 【数据结构与算法】03 链表(基础知识+面试高频leetcode题目)
目录 数组&链表基础 高频面试题(leetcode) 206 反转链表(常考) 方法1:迭代算法(双指针)(cpp&python) 方法2:递归解法 妖魔化的双指针算法. 24 两两交 ...
- l2-004 这是二叉搜索树吗?_LeetCode 例题精讲 | 11 二叉树转化为链表:二叉树遍历中的相邻结点...
本期例题: LeetCode 98. Validate Binary Search Tree 验证二叉搜索树(Medium) LeetCode 426. Convert Binary Tree to ...
- c语言关于链表选择题看不懂,有关链表基本操作三题
因为觉得C语言老师讲链表讲的太匆忙了,况且PPT太乱太杂,看不懂 因此特地重写了次这周三上机有关链表操作的三题.(所写题目默认已排序,不考虑排序) 问题A:在单链表中按学号查成绩 时间限制: 1 Se ...
- 循环双链表的手动构建总结
我们常用的是单链表的算法.虽然双链表和循环双链表的算法常常被拿来作为一种设计的思路,也常假想有一个循环双链表,对它进行操作,实际上我从没动手写过循环双链表. 在手动实现单链表的习题多道以后,有了一种对 ...
- c语言建立并存储树,利用十字链表存储树结构(便于同时求出某一点的入度与出度)------C语言实现...
#include #include #include /* 利用十字链表存储有向图,可用于同时查找某个顶点的出度与入度: */ typedef struct edge {//顶点表 int headv ...
- 【链表习题集1】整体和局部反转链表同频和快慢指针合并链表
前言: 刷题和面试兼顾还得看你啊-牛客网 近几年互联网受疫情影响,许多互联网都使用牛客网在线笔试招人 很多同学因为不熟悉牛客网的环境和使用,最后在线笔试面试中屡屡受挫 牛客网提供了语言巩固,算法提高等 ...
最新文章
- 2018年全球十大新兴技术:细胞植入人体、营养人造肉……你想试试吗?
- python中异常和错误是一个概念_Python的异常概念介绍以及处理
- SqlDataReader对象的NextResult方法读取存储过程多个结果集
- python二维列表写入excel_用Python实现合并excel列表
- 怎么真正入行Web前端行业?JavaScript五大新特性是什么?
- 远程无法连接数据库的问题
- 蓝桥杯 ADV-148算法提高 排队打水问题(贪心)
- 组织架构适配下的敏捷开发
- AUTOCAD——射线命令
- 短信验证码接口被恶意攻击怎么办?
- 用Changedetection监控网页的变化
- 基于asp.net的排球赛事网站设计与实现
- WEB安全——CS中Beacon的使用
- 复旦计算机课程谁的好,复旦大学计算机研究生导师有哪些
- VS2019生成DLL
- slub allocator工作原理
- 历史上的今天:“超级计算机之父”诞生;三星推出移动支付;LibreOffice 发布...
- Matlab信号添加噪声及信噪比SNR的计算
- 背后的力量 | 搭建新型IT基础架构 华云数据助力妇幼保健院提升数字化医院建设水平
- 为什么国家政府发的国债,被央行自己印的钱大量购买时,利率会降低呢?
热门文章
- extend_gcd求解不定方程/膜线性方程/乘法(模)逆元
- D3中数据与DOM element绑定之data() enter() exit()浅析
- 连网获取图片的小程序
- 【BZOJ】【3295】【CQOI2011】动态逆序对
- 朋友面试被问到---静态构造函数
- JavaScript 音频插件和图表插件
- 每个前端工程师都应该懂的前端性能优化总结:
- 入门机器学习(九)--应用机器学习的建议
- OpenCV3学习(3)——视频文件的读取与写入
- mysql 查询包含1或者2_Mysql:同一个表上有2个不同的查询,包含count和group by