链表4

public class ListNode {

    // 数据
    int val;
    // 下一个节点的引用
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }


    // 数组 转化成 链表
    public static ListNode arrayToListNode(int[] arr) {
        if (arr.length == 0) return null;

        // 生成链表的根节点
        ListNode root = new ListNode(arr[0]);

        ListNode pre = root;
        for (int i = 1; i < arr.length; i++) {
            ListNode node = new ListNode(arr[i]);
            // 创建链接关系  将前一个节点的next设置为当前节点
            pre.next = node;
            // 更新pre为当前节点  下一个要处理的节点
            pre = node;
        }

        return root;
    }


    public static void print(ListNode head) {
        while (head != null) {
            System.out.print(head.val);
            if (head.next != null) {
                System.out.print("->");
            }
            head = head.next;
        }
    }

    // pos 代表 尾节点指向 链表中某个节点的索引位置 (环的入口)
    public static void toCycle(ListNode node, int pos) {
        // 遍历  通过pos找到 入口对应的节点  记录下来
        // 遍历到尾节点时   设置为其next引用

        // 记录遍历的位置
        int cnt = 0;
        ListNode cycleNode = null;
        while (true) {
            // 判断是否是环的入口节点
            if (cnt == pos) {
                cycleNode = node;
            }

            // 判断是否是尾节点
            if (node.next == null) {
                node.next = cycleNode;
                return;
            }

            node = node.next;
            cnt++;
        }

    }
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值