链表常见问题

public class Node {

    Integer value;
    Node next;

    public Node(Integer value) {
        this.value = value;
    }

    public void print(Node head) {
        Node start = head;
        StringJoiner str = new StringJoiner(",");
        while (start != null) {
            str.add(start.value.toString());
            start = start.next;
        }
        System.out.println(str);
    }
}

1. 删除链表中的倒数第n个元素

class Test0712 {

    @Test
    void test01() {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(4);

        print(head);
        print(reversalLinkedList(head));

    }

    /**
     * 反转链表
     *
     * @param head 头
     * @return {@link my.Test0712.Node}
     */
    private Node reversalLinkedList(Node head) {
        Node newHead = null;
        Node current = head;
        while (current != null) {
            Node tmp = current.next;
            current.next = newHead;
            newHead = current;
            current = tmp;
        }
        return newHead;
    }
}

2. 链表中环的检测

class Test0712 {

    @Test
    void test02() {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(4);
        // head.next.next.next.next.next = head.next;

        System.out.println(hasCycle(head));
    }


    /**
     * 链表是否有环
     *
     * @param head 头
     * @return boolean
     */
    public static boolean hasCycle(Node head) {
       if (head == null || head.next == null) {
            return false;
        }

        Node slow = head;
        Node fast = head;

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

            if (slow == fast) {
                System.out.println(slow.value);
                return true;
            }
        }
        return false;
    }
}

3. 两个有序的链表合并

class Test0712 {

    @Test
    void test03() {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(4);

        Node head2 = new Node(7);
        head2.next = new Node(8);
        head2.next.next = new Node(9);

        // 0,1,2,3,4
        print(head);
        // 7,8,9
        print(head2);
        // 0,1,2,3,4,7,8,9
        print(mergeSortList(head, head2));
    }

    public Node mergeSortList(Node l1, Node l2) {
        if (l1 == null && l2 == null) {
            return null;
        }

        if (l1 == null) {
            return l2;
        }

        if (l2 == null) {
            return l1;
        }

        Node newHead = new Node(null);
        Node current = newHead;
        while (l1 != null && l2 != null) {
            if (l1.value <= l2.value) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }

        if (l1 != null) {
            current.next = l1;
        } else {
            current.next = l2;
        }
        return newHead;
    }
}

4. 删除链表倒数第 n 个结点(快慢指针)

class Test0712 {

    @Test
    public void test04() {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(4);

        print(head);
        print(removeNthFromEnd(head, 2));
    }

    /**
     * 删除倒数第n个节点
     *
     * @param head 头
     * @param n    n
     */
    private Node removeNthFromEnd(Node head, int n) {
        // 创建一个哑节点 (dummy node),指向头节点
        Node dummy = new Node(null);
        dummy.next = head;

        // 初始化两个指针,均指向哑节点
        Node first = dummy;
        Node second = dummy;

        // 将第一个指针向前移动 n+1 步
        for (int i = 0; i <= n; i++) {
            first = first.next;
        }

        // 移动两个指针,直到第一个指针到达链表末尾
        while (first != null) {
            first = first.next;
            second = second.next;
        }

        // 此时第二个指针指向要删除节点的前一个节点
        second.next = second.next.next;

        // 返回头节点(跳过哑节点)
        return dummy.next;
    }
}

5. 求链表的中间结点

class Test0712 {

    @Test
    public void test05() {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(4);

        print(head);
        System.out.println(middleNode(head).value);
    }

    public static Node middleNode(Node head) {
        Node slow = head;
        Node fast = head;

        // 使用快慢指针找到链表的中间节点
        while (fast != null && fast.next != null) {
            slow = slow.next;       // 慢指针每次移动一步
            fast = fast.next.next;  // 快指针每次移动两步
        }

        // 返回慢指针所指向的节点,即为中间节点
        return slow;
    }
|
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值