前言
刷题链接:
https://www.nowcoder.com/exam/oj/ta?page=2&tpId=13&type=265
1. 链表
JZ24 反转链表
![[图片]](https://img-blog.csdnimg.cn/f5a5462a1c8e47fa8cb9a4335117eb3b.png)
思路:基本操作,如下所示。
![[图片]](https://img-blog.csdnimg.cn/b81f7fa9b1004bb9ae7d2d4b798c2479.png)
![[图片]](https://img-blog.csdnimg.cn/3f998eed13bb413fa5c89456c014e7a9.png)
![[图片]](https://img-blog.csdnimg.cn/682fe91bd13b46f98db8fa7da9f1c54f.png)
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null){
return null;
}
ListNode pre = null;
ListNode cur = head;
ListNode temp = null;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
JZ6 从尾到头打印链表
![[图片]](https://img-blog.csdnimg.cn/24ee344dff1e40919152392e9310565a.png)
思路:
- 反转链表
- 添加元素至数组
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> arr = new ArrayList<Integer>();
// 1. 链表不为空,则反转链表
if(listNode!=null){
listNode = ReverseList(listNode);
// 2. 添加元素到ArrayList
ListNode cur = listNode;
while(cur!=null){
arr.add(cur.val);
cur = cur.next;
}
return arr;
}
return arr; // 空链表返回空ArrayList
}
public ListNode ReverseList(ListNode head){ //反转链表
ListNode pre = null;
ListNode cur = head;
ListNode temp = cur.next;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
JZ25 合并两个排序的链表
![[图片]](https://img-blog.csdnimg.cn/fbfe53e080f142b29e6e02b05019cb82.png)
![[图片]](https://img-blog.csdnimg.cn/feac183e27ab49749c2831a539c40a2a.png)
思路:看代码很清楚,利用新的lsit存排序后的链表,小的先存。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode list = new ListNode(0);
ListNode l = list;
if(list1 == null || list2 == null){
return list1 != null?list1:list2;
}
while(list1!=null && list2!=null){
if(list1.val<=list2.val){
l.next = list1;
list1=list1.next;
}else{
l.next = list2;
list2=list2.next;
}
l=l.next;
}
if(list1!=null){
l.next = list1;
}
if(list2!=null){
l.next = list2;
}
return list.next;
}
}
JZ52 两个链表的第一个公共节点
![[图片]](https://img-blog.csdnimg.cn/b13fa257ab474f8abaf6f68be84aaaaf.png)
![[图片]](https://img-blog.csdnimg.cn/63ecd9f3e02b47c4b5ddcdbb81b51714.png)
思路:Y字形链表,以相同的速度遍历两条链表最终总会相遇。(注意:当遍历链表1结束,将指针指向链表2的头。)
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode p1=pHead1, p2=pHead2;
while(p1!=p2){
p1 = (p1==null)?pHead2:p1.next;
p2 = (p2==null)?pHead1:p2.next;
}
return p1;
}
}
JZ23 链表中环的入口结点
![[图片]](https://img-blog.csdnimg.cn/c6cd828e22f145919e88ad1f276fb7a8.png)
![[图片]](https://img-blog.csdnimg.cn/6d76930b5a7b47f0b00f6a614cc137a8.png)
思路:
快慢指针,快指针走两步,慢指针走一步。
![[图片]](https://img-blog.csdnimg.cn/e815dcdd81db433f85a0908bacf1b31f.png)
- 判断是否有环;
- 有环则将slow指针指向头,fast从相遇点出发,二者将在入环点相遇。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
ListNode fast = pHead;
ListNode slow = pHead;
while(fast.next!=null && fast.next.next!=null){
fast = fast.next.next;
slow = slow.next;
if(fast==slow){ //判断有没有环
break;
}
}
if(fast.next==null || fast.next.next==null){ //无环返回null
return null;
}
slow = pHead;
while (slow != fast) { //找到环入口
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
JZ22 链表中倒数最后k个结点
![[图片]](https://img-blog.csdnimg.cn/0897850fb40947098eac544ed39f6fd3.png)
![[图片]](https://img-blog.csdnimg.cn/d68463dff2c647bb815b1fd9f25d65dc.png)
思路:快慢指针,先让一个指针走k步,后面同步一个个走,快指针走到末尾的时慢指针刚好指向末尾第k个节点。
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode FindKthToTail (ListNode pHead, int k) {
// write code here
if(pHead == null){
return null;
}
ListNode slow = pHead;
ListNode fast = pHead;
while(k-->0){
if(fast == null)
return null;
fast = fast.next; //先走k步
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
JZ35 复杂链表的复制
![[图片]](https://img-blog.csdnimg.cn/977e897d96e14499b656e4de0c8ad6cd.png)
![[图片]](https://img-blog.csdnimg.cn/2cfe05be42d0404da56d967d51a706ac.png)
思路:参考这篇博客 https://blog.csdn.net/qq_43676757/article/details/106253305
使用常规方法一,将步骤分成三个:
-
复制节点
![[图片]](https://img-blog.csdnimg.cn/bbf8edbebfed489992393293b269b695.png)
-
复制random节点给已拷贝节点
![[图片]](https://img-blog.csdnimg.cn/46a8c0adda3c4c87a723d629158650ea.png)
-
拆分链表
![[图片]](https://img-blog.csdnimg.cn/9125cc2dc08c47a99690126d86f2de84.png)
/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
public class Solution {
public RandomListNode Clone(RandomListNode pHead) {
if(pHead==null)
return null;
RandomListNode cur = pHead;
//遍历原始链表,复制
while(cur!=null){
//拷贝节点
RandomListNode temp = new RandomListNode(cur.label);
//添加新节点到原始链表当前节点后面
temp.next=cur.next;
cur.next=temp;
cur=temp.next;
}
//连接新链表的random节点
cur = pHead;//回到头节点
RandomListNode temp = pHead.next;
while(cur!=null){
if(cur.random==null){
temp.random=null;
}else{
temp.random = cur.random.next; //新链表的random为cur的random下一个
}
cur = cur.next.next;
if(temp.next!=null){
temp=temp.next.next;
}
}
//拆分链表
cur = pHead;
RandomListNode res = pHead.next;
temp = res;
while(cur!=null){
cur.next=cur.next.next;
cur=cur.next;
if(temp.next!=null){
temp.next = temp.next.next;
}
temp = temp.next;
}
return res;
}
}
JZ76 删除链表中重复的结点
![[图片]](https://img-blog.csdnimg.cn/3edbd15d207b4278a3acbe172a671c80.png)
![[图片]](https://img-blog.csdnimg.cn/616e6cbd307e422198c496ee75a10193.png)
思路:需要前一个节点、当前节点和下一节点的信息。在表头添加一个节点作为pre,当前节点和下一节点相同的时候,当前指针往下移动;当数值不同的时才退出循环,将pre.next指向下一个节点。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
// 链表为空或者只有一个节点
if(pHead == null || pHead.next==null){
return pHead;
}
ListNode res = new ListNode(-1); //表头加一个节点
res.next = pHead;
ListNode pre = res;
ListNode cur = pHead;
while(cur!=null && cur.next!=null){
if(cur.val == cur.next.val){ //相邻的节点值相同
int tmp = cur.val;
while(cur.next!=null && cur.next.val==tmp){
cur = cur.next; //跳过相同的节点
}
//退出循环时,cur指向最后一个重复值
cur = cur.next;
pre.next = cur;
}else{
pre=cur;
cur=cur.next;
}
}
return res.next;
}
}
JZ18 删除链表的节点
![[图片]](https://img-blog.csdnimg.cn/8e4cf335f88d4a7e91c011ef783d1ade.png)
思路:直观的想法就是记录下前一个节点,当搜索到匹配的数值,将前一个节点的next直接指向当前节点的下一个节点,达到删除当前节点的目的。特殊情况是表头节点满足条件!
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
public ListNode deleteNode (ListNode head, int val) {
// write code here
//1. 空则返回null
if(head == null){
return null;
}
ListNode pre = head; //pre记录当前节点前一个节点
ListNode cur = head.next;
//2.第一个元素就满足val,删除第一个元素
if(pre!=null&&pre.val==val){
pre=pre.next;
return pre;
}
//3.第二个元素开始匹配
while(cur!=null){
if(cur.val==val){
pre.next = cur.next;
cur.next = null;
break;
}
cur = cur.next;
pre = pre.next;
}
return head;
}
}
更简洁的代码(来源牛客网题解):在单链表前再加一个节点( ListNode res = new ListNode(0); res.next = head; ),返回的时候记得去掉。
import java.util.*;
public class Solution {
public ListNode deleteNode (ListNode head, int val) {
//加入一个头节点
ListNode res = new ListNode(0);
res.next = head;
//前序节点
ListNode pre = res;
//当前节点
ListNode cur = head;
//遍历链表
while(cur != null){
//找到目标节点
if(cur.val == val){
//断开连接
pre.next = cur.next;
break;
}
pre = cur;
cur = cur.next;
}
//返回去掉头节点
return res.next;
}
}


















