0
点赞
收藏
分享

微信扫一扫

【算法合集】学习算法第一天(链表篇)

Sophia的玲珑阁 2022-07-18 阅读 101

目录

🍟前言

🍟反转链表

🍔题解代码

🍟链表区间反转

🍔题解代码

🍟链表的奇偶重排

🍔题解代码

🍟链表中的节点每k个一组翻转

🍔题解代码

前言

 再看看某大厂的面试题

 

反转链表

🎀描述

 🎀示例1

 🎀示例2

题解代码

public class Solution {
    public ListNode ReverseList(ListNode head) {
        //处理空链表 fast-template
        if (head == null)
            return null;
        ListNode cur = head;
        ListNode pre = null;
        while (cur != null) {
            //断开链表,要记录后续一个
            ListNode temp = cur.next;
            //当前的next指向前一个
            cur.next = pre;
            //前一个更新为当前
            pre = cur;
            //当前更新为刚刚记录的后一个
            cur = temp;
        }
        return pre;}
}

链表内指定区间反转

💎描述

💎示例1

💎示例2

题解代码

import java.util.*;
public class Solution {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        //加个表头 fast-template
        ListNode res = new ListNode(-1);
        res.next = head;
        //前序节点
        ListNode pre = res;
        //当前节点
        ListNode cur = head;
        //找到m
        for (int i = 1; i < m; i++) {
            pre = cur;
            cur = cur.next;
        }
        //从m反转到n
        for (int i = m; i < n; i++) {
            ListNode temp = cur.next;
            cur.next = temp.next;
            temp.next = pre.next;
            pre.next = temp;
        }
        //返回去掉表头
        return res.next;}
}

链表的奇偶重排

💕描述

💕示例1

💕说明:

💕示例2

💕说明:

💕备注:

题解代码

import java.util.*;
public class Solution {
    public ListNode oddEvenList (ListNode head) { 
        //如果链表为空,不用重排 fast-template
        if(head == null)
            return head;
        //even开头指向第二个节点,可能为空
        ListNode even = head.next;
        //odd开头指向第一个节点
        ListNode odd = head;
        //指向even开头
        ListNode evenhead = even;
        while(even != null && even.next != null){
            //odd连接even的后一个,即奇数位
            odd.next = even.next;
            //odd进入后一个奇数位
            odd = odd.next;
            //even连接后一个奇数的后一位,即偶数位
            even.next = odd.next;
            //even进入后一个偶数位
            even = even.next;
        }
        //even整体接在odd后面
        odd.next = evenhead;
        return head;}
}

链表中的节点每k个一组翻转

🎉描述

🎉例如:

🎉示例1

🎉示例2

import java.util.*;
public class Solution {
    public ListNode reverseKGroup (ListNode head, int k) {
         //找到每次翻转的尾部 fast-template
        ListNode tail = head;
        //遍历k次到尾部
        for (int i = 0; i < k; i++) {
            //如果不足k到了链表尾,直接返回,不翻转
            if (tail == null)
                return head;
            tail = tail.next;
        }
        //翻转时需要的前序和当前节点
        ListNode pre = null;
        ListNode cur = head;
       //在到达当前段尾节点前
        while (cur != tail) {
            //翻转
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
       //当前尾指向下一段要翻转的链表
        head.next = reverseKGroup(tail, k);
        return pre;}
}

举报

相关推荐

暑期算法打卡----第一天

刷算法的第一天

第一天学习

RHCSA学习第一天

JavaScript学习第一天

【python学习】第一天

JVM学习_第一天

0 条评论