链表面试题

news/2024/4/30 14:29:21

删除链表中等于给定值 val 的所有节点

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode removeElements(ListNode head, int val) {if (head == null) {return head;}ListNode prev = head;ListNode cur = head.next;while (cur != null) {if (cur.val == val) {prev.next = cur.next;} else {prev = cur;}cur = cur.next;}if (head.val == val) {head = head.next;}return head;}
}

反转一个单链表

采用头插法即可,请看vcr 

reverseLinkedList

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseList(ListNode head) {if (head == null)return head;ListNode cur = head.next;head.next = null;while (cur != null) {ListNode curN = cur.next;//开始反转cur.next = head;head = cur;cur = curN;}return head;}
}

给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点

快慢指针:

偶数长:fast==null                             奇数长:fast.next==null

数学问题:

路程一样的情况下,速度是2倍,那么当走完的时候,路程也是两倍。

快慢指针-偶数长

快慢指针-奇数长

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode middleNode(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;}return slow;}
}

输入一个链表,输出该链表中倒数第k个结点

快慢指针

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
class Solution {public int kthToLast(ListNode head, int k) {ListNode fast = head;ListNode slow = head;// 1.先让fast走k-1步for (int i = 0; i <= k - 1; i++) {fast = fast.next;}// 2.然后一起走,当fast走到最后一个节点的时候,slow的位置就是倒数第k个节点while (fast != null) {fast = fast.next;slow = slow.next;}return slow.val;}
}

难度升级:如果k不合法,该怎么写?

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
class Solution {public int kthToLast(ListNode head, int k) {if (k <= 0) {return -1;}ListNode fast = head;ListNode slow = head;// 1.先让fast走k-1步for (int i = 0; i <= k - 1; i++) {if (fast == null) {return -1;}fast = fast.next;}// 2.然后一起走,当fast走到最后一个节点的时候,slow的位置就是倒数第k个节点while (fast != null) {fast = fast.next;slow = slow.next;}return slow.val;}
}

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的

合并两对有序数

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val = val; }* ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode mergeTwoLists(ListNode headA, ListNode headB) {ListNode newH = new ListNode();ListNode temp = new ListNode();newH = temp;while (headA != null && headB != null) {if (headA.val < headB.val) {temp.next = headA;headA = headA.next;temp = temp.next;} else {temp.next = headB;headB = headB.next;temp = temp.next;}}if (headA == null)temp.next = headB;elsetemp.next = headA;return newH.next;}
}

编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前

1.遍历链表

2.把对应的节点放到指定区间

3.把两个区间连起来

分割指针

import java.util.*;/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class Partition {public ListNode partition(ListNode pHead, int x) {if (pHead == null) {return null;}ListNode bs = null, be = null, as = null, ae = null;ListNode cur = pHead;while (cur != null) {if (cur.val < x) {if (bs == null) {//第一次插入bs = be = cur;} else {cur.next = be;be = be.next;}} else {//第一次插入if (as == null) {as = ae = cur;} else {cur.next = ae;ae = ae.next;}}cur = cur.next;}//进行拼接be.next = as;return be;}
}

此代码存在的问题:

1.最后一个节点没有了null

2.全部小于x 全部大于x

import java.util.*;/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class Partition {public ListNode partition(ListNode pHead, int x) {if (pHead == null) {return null;}// write code hereListNode bs = null;ListNode be = null;ListNode as = null;ListNode ae = null;ListNode cur = pHead;while (cur != null) {if (cur.val < x) {//第一次插入if (bs == null) {bs = be = cur;} else {be.next = cur;be = be.next;}cur = cur.next;} else {if (as == null) {//第一次插入as = ae = cur;} else {ae.next = cur;ae = ae.next;}cur = cur.next;}}//第一部分是不是空的,如果空 返回第二部分,如果不为空 进行拼接if (bs == null) {return as;}//进行拼接be.next = as;//后半部分不为空 把后半部分 最后一个节点置空if (as != null) {ae.next = null;}return bs;}
}

链表的回文结构

链表的回文结构

但是偶数节点,head和slow没办法相遇

import java.util.*;/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class PalindromeList {public boolean chkPalindrome(ListNode head) {if (head == null) {return true;}//1.遍历链表ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;}//2.翻转中间节点后面的链表ListNode cur = slow.next;while (cur != null) {ListNode curN = cur.next;cur.next = slow;slow = cur;cur = curN;}//3.slow从后往前,head从前往后,直到他俩相遇while (head != slow) {if (head.val != slow.val) {return false;}if (head.next == slow) {return true;}head = head.next;slow = slow.next;}return true;}
}

输入两个链表,找出它们的第一个公共结点

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {// 1.假设A链表长ListNode pl = headA;ListNode ps = headB;int lenA = 0;int lenB = 0;while (pl != null) {lenA++;pl = pl.next;}while (ps != null) {lenB++;ps = ps.next;}// 要重新赋值pl = headA;ps = headB;int len = lenA - lenB;// 2.判断len是正数还是负数if (len < 0) {pl = headB;ps = headA;len = lenB - lenA;}// 上述两步走完之后// pl一定指向最长的链表,ps一定指向最短的链表// len 一定是整数// 3.让长链表pl走差值len步while (len != 0) {pl = pl.next;len--;}// 一起走 直到相遇while (pl != ps) {pl = pl.next;ps = ps.next;}if (pl == null) {// 如果两个引用都为空 证明不相交 虽然不写也没问题return null;}return pl;}
}

给定一个链表,判断链表中是否有环

快慢指针,即慢指针一次走一步,快指针一次走两步,两个指针从链表起始位置开始运行,如果链表带环则一定会在环中相遇,否则快指针率先走到链表的末尾。

/*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public boolean hasCycle(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {return true;}}return false;}
}

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL

/*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public ListNode detectCycle(ListNode head) {ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {break;}}if (fast == null || fast.next == null) {return null;}fast = head;while (fast != slow) {fast = fast.next;slow = slow.next;}return fast;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/11537.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程

引言劝退 VSCode&#xff0c;全称为Visual Studio Code&#xff0c;是由微软开发的一款轻量级&#xff0c;跨平台的代码编辑器。大家能来搜用VSCode配置c/c&#xff0c;想必也知道VSCode的强大&#xff0c;可以手握一个VSCode同时编写如C&#xff0c;C&#xff0c;C#&#xff…

nginx的https与动态负载均衡

nginx的https 证书可以根据你的域名和服务器服务商去进行签发 , 比如 : 阿里云 腾讯云 百度云 华为云等 这里使用的是腾讯云 : 下载证书 : 选择 nginx: 下载之后传递到服务器上。 下面开始配置nginx的https: 1. 解压下载的证书包 cd /etc/ssl unzip xxcc.dwa_nginx.zip mv…

鸿蒙OS开发实例:【应用事件打点】

简介 传统的日志系统里汇聚了整个设备上所有程序运行的过程流水日志&#xff0c;难以识别其中的关键信息。因此&#xff0c;应用开发者需要一种数据打点机制&#xff0c;用来评估如访问数、日活、用户操作习惯以及影响用户使用的关键因素等关键信息。 HiAppEvent是在系统层面…

安装Docker(CentOS)

Docker 分为 CE 和 EE 两大版本。CE 即社区版&#xff08;免费&#xff0c;支持周期 7 个月&#xff09;&#xff0c;EE 即企业版&#xff0c;强调安全&#xff0c;付费使用&#xff0c;支持周期 24 个月。 Docker CE 分为 stable test 和 nightly 三个更新频道。 官方网站上…

蓝桥杯 - 受伤的皇后

解题思路&#xff1a; 递归 回溯&#xff08;n皇后问题的变种&#xff09; 在 N 皇后问题的解决方案中&#xff0c;我们是从棋盘的顶部向底部逐行放置皇后的&#xff0c;这意味着在任何给定时间&#xff0c;所有未来的行&#xff08;即当前行之下的所有行&#xff09;都还没…

MySQL数据库(一)

文章目录 1.MySQL8.0安装配置1.安装教程2.启动方法3.启动注意事项4.Navicat使用5.Navicat演示 2.MySQL数据库基本介绍1.三层结构2.SQL语句分类 3.MySQL数据库基本操作1.创建数据库2.不区分大小写的校对规则3.查看、删除数据库4.备份和恢复数据库1.备份数据库db01和db02&#xf…