链表分割,链表的回文结构等经典例题解析!

本文介绍了如何在Java中实现链表的分割功能,通过`partition`方法将链表分为两部分,以及使用快慢指针判断链表是否是回文结构的`chkPalindrome`方法。
摘要由CSDN通过智能技术生成

1.链表的分割

链接:链表分割_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70?tpId=8&&tqId=11004&rp=2&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking

public class Partition {

    public ListNode partition(ListNode pHead, int x) {

        ListNode cur = pHead;

        ListNode bs = null;

        ListNode be = null;

        ListNode as = null;

        ListNode ae = null

        if(pHead == null) {

            return null;

        }

        while (cur != null) {     //使用cur遍历所有节点

            if(cur.val < x) {

                if(bs == null) {

                    bs = cur;

                    be = cur;

                }else {

                    be.next = cur;

                    be = be.next;

                }

            }else {

                // cur.val >= x

                if(as == null) {

                    as = cur;

                    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;

    }

}

2.链表的回文结构

链接:

链表的回文结构_牛客题霸_牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa?tpId=49&&tqId=29370&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking//快慢指针求解

import java.util.*;

/*

public class ListNode {

    int val;

    ListNode next = null;

    ListNode(int val) {

        this.val = val;

    }

}*/

public class PalindromeList {

    public boolean chkPalindrome(ListNode A) {

        ListNode cur = null;

        ListNode slow = A;

        ListNode fast = A.next;

        //找中间节点

        while(fast != null && fast.next != null) {

            slow = slow.next;

            fast = fast.next;

        }

        ListNode Middle = slow;

       //翻转中心节点之后的链表值

            while(cur != null) {

            cur = Middle.next;

            ListNode curNext = cur.next;

            curNext.next = cur;

            cur = curNext;

            Middle = Middle.next;

        }

        cur.next = null;

//判断从链表开头,与中心节点之后的数值是否相等

        while(slow != null) {

            if(A.val == slow.val) {

                A = A.next;

                slow = slow.next;

            }else {

                return false;

            }

        }

        return true;

    }

}

  • 32
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值