文章目录
🥇1.反转链表
在以下的问题中都需要一个测试类,TestDemo.java文件
,测试方法功能。以及实现链表功能的函数,这些功能方法都存放在MyLinkedList.java
文件中
TestDemo.java
文件:
public class TestDmeo {
public static void main(String[] args) {
MylinkedList mylinkedList = new MylinkedList();
}
}
在MyLinkedList.java
文件中新建一个链表和打印链表
public Node head; //建立头结点
public void createList() {
Node node1 = new Node(6);
Node node2 = new Node(1);
Node node3 = new Node(8);
Node node4 = new Node(7);
Node node5 = new Node(10);
//把每个节点穿起来
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
this.head = node1;
}
//实现打印链表
public void print(){
if(this.head == null){
return;
}
Node cur = this.head;
while(cur!=null){
System.out.print(cur.val+" ");
cur = cur.next;
}
}
接下来就到了实现以下功能的时候了
反转链表
🚩那让我们先看看翻转后的链表吧
📄算法思想之尾插法:
- 判断链表是否为空,为空就返回null
- 如果链表中只有一个节点,那就直接返回这个节点
- 建立一个新的头节点并把它赋为null 即 Node newhead = null
- 建立跟随节点cur,并赋予
cur = this.head
- 既然要翻转链表,那么我们需要把原链表的头结点的next 赋为 null。即cur.next = null,因为在翻转链表过程中要遍历链表所以我们要保存节点Node
curNext = cur.next
,翻转链表cur.next = newhead
,然后新头结点也依次向后遍历newhead = cur
,跟随节点遍历链表cur = curNext
;直到cur==null为止,遍历链表结束
🚩老规矩,看图说话:
💯代码:
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null)//如果链表为null,或者链表中只有一个节点
{
return head;
}
//建立新的头结点
ListNode newhead = null;
//建立跟随节点
ListNode cur = head;
while(cur!=null){
ListNode curNext = cur.next; //保存节点
cur.next = newhead; //把旧的头结点 this.head.next = null
newhead = cur;
cur = curNext;
}
return newhead;
}
}
🥇2.返回链表的中间节点
📄算法思想:
1. 判断链表是否为空;
2. 用快慢指针的方法解答此题,先把快指针和慢指针都初始化为this.head,即Node fast = this.head,Node slow = this.head
;
3. 然后快指针在链表中一次遍历两步,慢指针一次遍历一步
,即fast = fast.next.next;slow = slow.next
就这样当快指针把链表遍历完的时候,慢指针刚刚遍历到链表的一半,即慢指针在链表的中间节点,返回这个中间节点
4. 遍历条件:单链表中节点个数为奇数
时,限制 条件为fast.next!=null
,当节点个数为偶数
时,限制条件为fast!=null
🚩看图说话:
💯代码:
class Solution {
public ListNode middleNode(ListNode head) {
if(head == null){
return head;
}
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
🥇3.返回链表中倒数第k个节点
📄算法思想:
1. 先判断链表是否为空,为空就返回null
2. 还是依据快慢指针的思想,让快指针先走k-1位
,然后快指针和慢指针一块一步走
,直到把链表遍历完
🚩看图说话:
💯代码:
class Solution {
public int kthToLast(ListNode head, int k) {
if(head == null){
return -1;
}
ListNode fast = head;
ListNode slow = head;
int count = 0;
while(count!=k-1 && fast!=null && fast.next!=null){
count++;
fast = fast.next;
}
while(fast!=null && fast.next != null){
fast = fast.next;
slow = slow.next;
}
return slow.val;
}
}
🥇4.判断链表是否有环
📄算法思想:
- 判断链表是否为空
- 利用快慢指针思想,定义快慢指针,
起先都赋为head
,快指针一次走两步,慢指针一次走一步
,如果在遍历链表的时候快指针节点的地址和慢指针节点的地址相同
,则证明链表有环
🚩看图说话:
💯代码:
public class Solution {
public boolean hasCycle(ListNode head) {
//判断链表是否有环
ListNode slow = head; //定义慢指针
ListNode fast = head; // 定义快指针
while(fast!=null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(slow == fast){
return true;
}
}
return false;
}
}
🥇5.合并两个有序链表,为一个有序大链表
📄算法思想:
1. 已知两个链表中节点中的数值为升序排序。
2. 当其中一个链表为空时,返回另一个链表,当两个升序链表都为空时,返回null
3. 定义一个虚拟节点newhead
,在定义一个跟随节点temp
,两个链表中的节点的数值进行比较,较小的进入新链表,例如假设此时headA节点的数值较小那么,temp.next = headA
,然后原链表继续向后遍历headA = headA.next
,跟随节点再遍历下一个链表节点temp = temp.next
🚩看图说话:
💯代码:
class Solution {
public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
//建立虚拟节点
//建立长链表跟随节点
ListNode newhead = new ListNode(-1);
ListNode temp = newhead;
if(headA == null){
return headB;
}
if(headB == null){
return headA;
}
if(headA == null && headB == null){
return null;
}
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;
}
if(headB == null){
temp.next = headA;
}
return newhead.next;
}
}
🥇6.已知链表有环,返回链表的入环节点
📄算法思想:
1. 判断链表是否为空,为空就返回null
2. 还是根据快慢指针的思想,慢指针slow = head,快指针fast =head,快指针一次遍历两个节点,慢指针一次遍历一个节点,快慢指针遍历链表,如果在遍历途中fast == slow 说明链表有环,否则无环,相遇的时候说明快指针遍历的链表长度是慢指针遍历的两倍,当相遇的时候,把slow置为head,slow和fast一块一步走,直到fast == slow
🚩看图说话:
💯代码:
public class Solution {
public ListNode detectCycle(ListNode head) {
//如果链表为空
if(head == null){
return null;
}
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
break;
}
}
if(fast == null || fast.next == null){
return null;
}
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
🥇7.判断链表是否是回文链表
📄算法思想:
1. 先找到链表的中间节点
2. 翻转中间节点之后的链表
3. 分别利用两个指针遍历没有翻转的一小段链表和翻转过得一小段链表,并且比较他们节点中的数值是否相等
🚩看图说话:
💯代码:
class Solution {
public boolean isPalindrome(ListNode head) {
//先找到中间节点,再把中间节点之后的节点进行翻转链表,然后对撞指针
//定义快慢指针
if(head == null){
return false;
}
ListNode fast = head;
ListNode slow = head;
//快指针一次走两步,慢指针一次走一步
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
//找到中间节点
//返回中间节点之后的链表
//定义一个跟随节点
ListNode cur = slow.next;
while(cur!= null){
//保存节点
ListNode curNext = cur.next;
//翻转、
cur.next = slow;
slow = cur;
cur = curNext;
}
//翻转完毕
//对撞
while(head!=slow){
if(head.val != slow.val){
return false;
}
if(head.next == slow){
return true;
}
head = head.next;
slow = slow.next;
}
if(head == slow){
return true;
}
return false;
}
}
🥇8.分割链表
📄算法思想:
1. 判断被分割链表是否为空,为空返回null
2. 分别建立两个新的头节点headAstart
和headBstart
,和尾节点headAend
和headBend
3. 在被分割链表中遍历,把小于固定值的节点存放在链表headAstart中,headAend在子链表A中遍历
,大于固定值的节点存放在链表headBstart中,headBend在子链表B中遍历
。
4. 最后连接两个链表,headAend.next = headBstart
;,把子链表headBstart中的尾节点next赋为null
🚩看图说话:
💯代码:
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
//算法思想:建立两个子链表,当在主链表中遍历的时候,遍历到比
//x小的节点时,把这个节点添加到子链表headA中,大于x添加到子
//链表headB中,然后两个节点进行拼接
ListNode cur = pHead; //建立主链表跟随节点
ListNode headAstart = null;
ListNode headAend = null;
ListNode headBstart = null;
ListNode headBend = null;
while(cur!=null){
if(cur.val < x){
if(headAstart == null){
headAstart = cur;
headAend = cur;
}else{
headAend.next = cur;
headAend = headAend.next;
}
}else{
if(headBstart == null){
headBstart = cur;
headBend = cur;
}else{
headBend.next = cur;
headBend = headBend.next;
}
}
cur = cur.next;
}
if(headAstart == null){
return headBstart;
}
if(headBstart == null){
return headAstart;
}
headAend.next = headBstart;
if(headBend!=null){
headBend.next = null;
}
return headAstart;
}
}
🥇9.删除链表的中间节点
注意这道题,很搞人哟,一定要仔细读题。要不然就和博主起先一样被耍了,以为要找到链表的真正中间节点,在进行删除,结果就被耍了,题目中的中间节点的意思是 除头节点和尾节点都算中间节点,读到这里有没有恍然大悟呀,哈哈哈
📄回来了,回来了,看看它的算法思想:
1. 在所传来的参数中node表示的是要删除的节点,所以我们要删除这个节点。
2. 让删除节点后的节点,覆盖删除节点
。node.val = node.next.val;node.next = node.next.next
;
🚩看图说话:
💯代码:
class Solution {
public void deleteNode(ListNode node) {
//如果删除节点为头结点
//删除节点
node.val = node.next.val;
node.next = node.next.next;
}
}
🥇10.链表相交,返回链表链表的相交节点
📄算法思想:
1. 先判断链表是否为空,一共有两个链表,如果其中有一个链表为空的话,那么就们有相交节点,就返回null
2. 跟随节点cur,遍历链表,计算两个链表的长度lenA,lenB,默认lenA计算的是长链表的长度,lenB计算的是短链表的长度
,pl = this,head
,ps = this,head
,让长链表先走lenA-lenB
步,然后pl和ps就到了同一起跑线
,让ps和pl一块走
,直到pl == ps
退出
🚩看图说话:
💯代码:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//当个链表长度不一样时,求出差值
int lenA = 0;
int lenB = 0;
ListNode pl = headA;
ListNode ps = headB;
while(pl!=null){
lenA++;
pl = pl.next;
}
while(ps!=null){
lenB++;
ps = ps.next;
}
//恢复节点
pl = headA;
ps = headB;
int len = lenA - lenB;
if(len<0){
pl = headB;
ps = headA;
len = lenB - lenA;
}
//长链表先走差值步
for(int i = 0;i<len;i++){
pl = pl.next;
}
//同时走
while(pl!=ps && pl != null && ps != null){
pl = pl.next;
ps = ps.next;
}
if(pl == ps && ps != null && pl != null){
return pl;
}
return null;
}
}
🥇11.从尾到头打印链表
📄算法思想:
1. 判断链表是否为空
2. 遍历链表,利用循环从尾到头打印链表,以数组形式返回
🚩看图说话:
💯代码:
class Solution {
public int[] reversePrint(ListNode head) {
//遍历链表,求出链表长度
int len = 0;
ListNode cur = head;
while(cur!=null){
len++;
cur = cur.next;
}
cur = head;
int i = 0;
int []array = new int[len];
for(i = len-1;i>=0;i--){
array[i] = cur.val;
cur = cur.next;
}
return array;
}
}
🥇12.删除链表中重复的节点
📄算法思想:
1. 判断链表是否为空,为空就返回null
2. 创建一个虚拟节点,防止第一个节点中的数值,为重复数值
3. 遍历链表,如果遇到相同的节点就跳过。cur.val == cur.next.val
就跳过
🚩看图说话:
💯代码:
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
//创建一个虚拟节点
ListNode newhead = new ListNode(-1);
ListNode temp = newhead;
ListNode cur = pHead; //创建跟随节点
while(cur!=null){
//如果遇到节点数值相同的数,就跳过
if(cur.next!=null && cur.val == cur.next.val){
while(cur.next!=null && cur.val == cur.next.val){
cur = cur.next;
}
cur = cur.next;
}else{
temp.next = cur;
temp = temp.next;
cur = cur.next;
}
}
temp.next = null;
return newhead.next;
}
}
ps 记得点 👍哦, 🤞