程序员社区

算法-面试题系列﹎﹎链表相关面试题

快慢指针解法

1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点

// head 头
    public static Node midOrUpMidNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return head;
        }
        // 链表有3个点或以上
        Node slow = head.next;
        Node fast = head.next.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
  1. 输入链表头节点,奇数长度返回中点,偶数长度返回下中点
public static Node midOrDownMidNode(Node head) {
        if (head == null || head.next == null) {
            return head;
        }
        Node slow = head.next;
        Node fast = head.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

3)输入链表头节点,奇数长度返回中点前一个,偶数长度返回上中点前一个

    public static Node midOrUpMidPreNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        Node slow = head;
        Node fast = head.next.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

4)输入链表头节点,奇数长度返回中点前一个,偶数长度返回下中点前一个

public static Node midOrDownMidPreNode(Node head) {
        if (head == null || head.next == null) {
            return null;
        }
        if (head.next.next == null) {
            return head;
        }
        Node slow = head;
        Node fast = head.next;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }


题目:给定一个单链表的头节点head,请判断该链表是否为回文结构。

12321 1221 回文结构

1)哈希表方法特别简单(笔试用)

算法-面试题系列﹎﹎链表相关面试题插图
image-20210512211936527
算法-面试题系列﹎﹎链表相关面试题插图1
image-20210512212139648
public static boolean isPalindrome1(Node head) {
        Stack<Node> stack = new Stack<Node>();
        Node cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (head != null) {
            if (head.value != stack.pop().value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

2)改原链表的方法就需要注意边界了(面试用)

算法-面试题系列﹎﹎链表相关面试题插图2
image-20210512213348160
public static boolean isPalindrome2(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Node right = head.next;
        Node cur = head;
        while (cur.next != null && cur.next.next != null) {
            right = right.next;
            cur = cur.next.next;
        }
        Stack<Node> stack = new Stack<Node>();
        while (right != null) {
            stack.push(right);
            right = right.next;
        }
        while (!stack.isEmpty()) {
            if (head.value != stack.pop().value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }
赞(0) 打赏
未经允许不得转载:IDEA激活码 » 算法-面试题系列﹎﹎链表相关面试题

一个分享Java & Python知识的社区