链表

回文链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        if (head == null) {
            return true;
        }
        if (head.next == null) {
            return true;
        }
        ListNode mid = getMid(head);
        ListNode n1 = revert(mid.next);
        while (n1 != null) {
            if (head.val != n1.val) {
                return false;
            }
            head = head.next;
            n1 = n1.next;
        }
        return true;
    }

    public ListNode revert(ListNode head) {
        if (head.next == null) {
            return head;
        }
        ListNode next = revert(head.next);
        head.next.next = head;
        head.next = null;
        return next;
    }

    public ListNode getMid(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null) {
            fast = fast.next;
            if (fast != null && fast.next != null ) {
                fast = fast.next;
                slow = slow.next;
            }
        }
        return slow;
    }
}

重排链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reorderList(ListNode head) {
        if (head == null) {
            return;
        }
        ListNode mid = getMid(head);
        ListNode temp = mid.next;
        mid.next = null;
        ListNode node1 = revertStack(temp);
        ListNode c = head;
        while (node1 != null) {
            temp = node1.next;
            node1.next = c.next;
            c.next = node1;
            c = node1.next;
            node1 = temp;
        }
    }

    public ListNode getMid(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode revertStack(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = revertStack(head.next);
        head.next.next = head;
        head.next = null;
        return next;
    }

    public ListNode revertLocal(ListNode head) {
        ListNode emptyHead = new ListNode(-1, head);
        ListNode current = emptyHead.next;
        ListNode next = emptyHead.next.next;
        while (next != null) {
            ListNode temp = next.next;
            next.next = current;
            current = next;
            next = temp;
        }
        return head;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值