反转链表、合并两个有序链表、回文链表 (来源力扣)

1、给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

方法1:递归

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode p = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return p;
    }
}

往往递归的思想和代码不容易看懂。就本题而言,首先定义递归函数的意义:即 reverseList(head)的含义是head后面的结点都已经反向,并且返回了反向后的新的头节点。顺着自己定义的递归函数的意义来理解递归代码和思想就容易多了,即head前的结点都未进行反向处理,而head后的结点都已经进行过反向处理,接着就可以 head.next.next = head; head.next = null; 即head结点的处理。

2、将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

方法1:递归

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
}

本题也可以使用递归来实现两个有序链表的合并,首先定义递归函数的意义,即mergeTwoLists(ListNode l1,ListNode l2)是返回 l1 和 l2 中首结点较小值的结点。

3、给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

输入:head = [1,2,2,1]
输出:true

进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

方法1:利用快慢指针将链表分为两半,将后一半逆置,利用双指针逐一判断

class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head == null)
            return true;
        ListNode mid = mid(head);
        ListNode tail = reverseList(mid.next);
        ListNode tmp1 = head;
        ListNode tmp2 = tail;
        boolean flag = true;
        while(flag && tmp2 != null) {
            if(tmp1.val != tmp2.val)
                return false;
            tmp1 = tmp1.next;
            tmp2 = tmp2.next;
        }
        return true;
    }
    private ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode next;
        while(cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
    private ListNode mid(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

方法2:递归

class Solution {
    private ListNode frontPointer;

    private boolean recursivelyCheck(ListNode currentNode) {
        if (currentNode != null) {
            if (!recursivelyCheck(currentNode.next)) {
                return false;
            }
            if (currentNode.val != frontPointer.val) {
                return false;
            }
            frontPointer = frontPointer.next;
        }
        return true;
    }

    public boolean isPalindrome(ListNode head) {
        frontPointer = head;
        return recursivelyCheck(head);
    }
}

本题的递归函数recursivelyCheck(ListNode head)的含义是判断链表head是否为递归链表。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值