链表题目汇总(反转链表 回文链表 链表中点)

目录

链表全部反转

链表从m到n反转

链表中点

回文链表


反转链表是面试中经常会问到的题目,可以作为模板理解后记忆。

主要有两道,一个是将一个链表全部反转,一个是将链表的m到n个节点反转。

这些模板可以应用在很多题目中比如

234. 回文链表icon-default.png?t=M0H8https://leetcode-cn.com/problems/palindrome-linked-list/

 

链表全部反转

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null) return head;
        ListNode cur = head;// cur指向当前节点
        ListNode pre = null;// pre保存上一个节点  遍历时将cur指向pre即可
        while(cur != null){
            ListNode nextNode = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nextNode;
        }
        return pre;
    }
}

链表从m到n反转

public class ListNodeTest {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        printNode(head);
        ListNode node = ReverseListmn(head, 2, 3);
        printNode(node);
    }
    public static ListNode ReverseListmn(ListNode head, int m, int n) {
        if(head==null || m>=n) return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;// cur指向哑结点
        int k = n - m + 1;//有k个节点需要进行反转操作
        while(m-- > 1){
            cur = cur.next;
        }
        ListNode firstPart = cur;//第m各节点的前一个节点
        cur = cur.next;//第m个节点
        ListNode tail = cur;//第m个节点
        ListNode pre = null;// pre保存上一个节点  遍历时将cur指向pre即可
        ListNode nextNode = null;
        while(k-- > 0 && cur != null){
            nextNode = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nextNode;
        }
        firstPart.next = pre;
        tail.next = nextNode;
        return dummy.next;
    }
    public static void printNode(ListNode node){
        while(node != null){
            System.out.print(node.val+"->");
            node = node.next;
        }
        System.out.println("null");
    }
}

链表中点

快慢指针

public ListNode middleNode(ListNode head) {
        if(head==null) return null; 
        ListNode slow = head;
        ListNode fast = head;
// fast 每次走两步,slow 每次走一步 
// 跳出条件 fast.next == null 说明链表节点数是奇数,此时slow节点位于中间节点
// fast.next.next == null 说明链表节点数是偶数,此时slow位于中间两个节点的左边那一个
//  可以用 1 -> 2 -> 3     1 -> 2 -> 3 -> 4 来帮助理解
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

回文链表

class Solution {
    public boolean isPalindrome(ListNode head) {
        if (head == null) {
            return true;
        }

        // 找到前半部分链表的尾节点并反转后半部分链表
        ListNode firstHalfEnd = endOfFirstHalf(head);
        ListNode secondHalfStart = reverseList(firstHalfEnd.next);

        // 判断是否回文
        ListNode p1 = head;
        ListNode p2 = secondHalfStart;
        boolean result = true;
        while (result && p2 != null) {
            if (p1.val != p2.val) {
                result = false;
            }
            p1 = p1.next;
            p2 = p2.next;
        }        

        // 还原链表并返回结果
        firstHalfEnd.next = reverseList(secondHalfStart);
        return result;
    }

    private ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    private ListNode endOfFirstHalf(ListNode head) {
        ListNode fast = head;// 利用快慢指针找到链表的中间节点
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

trigger333

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值