美文网首页
如何根据数组或者字符串创建链表?

如何根据数组或者字符串创建链表?

作者: Coder_LL | 来源:发表于2021-03-28 18:50 被阅读0次

By Long Luo

Leetcode链表相关的题时,给出的测试用例总是数组或者字符串形式,比如61. 旋转链表这道题,Testcase如下所示:

示例 1:

img

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

问题

如果在本地测试代码时,每次测试代码的时候,都需要自己创建测试用例,或者调用leetcode给的例子,都需要手动去创建一个链表的话,但这样效率太低!针对这种情况,写了几个工具函数用来处理链表的代码,可供大家参考。

测试代码

个人测试链表测试代码如下:

public static void main(String[] args) {
    ListNode test1 = LinkedListUtils.constructListNode(new int[]{1, 2, 3, 4, 5});
    System.out.println("[4,5,1,2,3] ?=" + LinkedListUtils.printLinkedList(rotateRight(test1, 2)));

    ListNode test2 = LinkedListUtils.constructListNode(new int[]{0, 1, 2});
    System.out.println("[2,0,1] ?=" + LinkedListUtils.printLinkedList(rotateRight(test2, 4)));
}

打印链表

Leetcode上链表最后输出形式:

输出:[4,5,1,2,3]

打印方法很简单,代码如下所示:

    public static String printLinkedList(ListNode head) {
        if (head == null) {
            return "[]";
        } else if (head.next == null) {
            return "[" + head.val + "]";
        }

        StringBuilder sb = new StringBuilder();
        sb.append("[");
        while (head.next != null) {
            sb.append(head.val);
            if (head != null) {
                sb.append(",");
            }
            head = head.next;
        }
        sb.append(head.val).append("]");

        return sb.toString();
    }

如何创建一个链表?

通过int数组创建链表

由一个给定的数组来创建一个链表,这个很简单,只需要遍历输入的数组,然后根据各个元素创建链表的节点的对象,然后把各个对象链接起来即可。

    public static ListNode constructListNode(int[] numbers) {
        if (numbers == null || numbers.length == 0) {
            return null;
        }

        ListNode dummyNode = new ListNode(-1);
        ListNode preNode = dummyNode;
        for (int i = 0; i < numbers.length; i++) {
            ListNode currNode = new ListNode(numbers[i]);
            preNode.next = currNode;
            preNode = preNode.next;
        }

        return dummyNode.next;
    }

注意:这里使用了链表操作常用的技巧,使用一个dummyNode作为链表的起始节点。这样做的好处是使后面所有节点的操作完全相同,否则链表中的第一个节点的操作要特殊处理,这样可以减少冗余代码,提高效率(不同多次判断是否为头节点),并且即使为空链表,也不需要特殊考虑。

通过字符串创建链表

上一小节,我们实现了通过int数组得到了链表的方法,但Leetcode中还有通过字符串来构建链表,而字符串又有多种形式。总结有以下几种方式:

"1->2->3->4->5->NULL"
"1->2->3->4->5->null "
" 1->2->3->4->5"
"[1, 2, 3, 4 ,5]"
"[1, 2, 3, 4 ,5, NULL]"
"[1, 2, 3, 4 ,5, null]"

注意:
   1. 字符串前后中间可以有空格
   2. NULL为大写或小写均可

有了上一节构造链表的方法,我们可以先将字符串处理成整型数组,然后调用constructListNode(int[] numbers)来构造链表。

处理思路如下:

  1. 去除字符串中全部空格;
  2. 如果前后有中括号[],先去除括号,再按照逗号,调用split进行分割数组;
  3. 如果中间有箭头->,调用split进行分割数组;
  4. 遇到最后的NULL或者null,设置为空。

代码如下所示:

    public static ListNode constructListNode(String str) {
        if (str == null || str.length() == 0) {
            return null;
        }

        String listStr = str.replaceAll(" ", "");
        String[] numbersStrArray;
        if (listStr.charAt(0) == '[' && listStr.charAt(listStr.length() - 1) == ']') {
            listStr = listStr.substring(1, listStr.length() - 1);
            numbersStrArray = listStr.split(",");
        } else if (listStr.contains("->")) {
            numbersStrArray = listStr.split("->");
        } else {
            numbersStrArray = new String[1];
            numbersStrArray[0] = listStr;
        }

        int numLength = 0;
        if (numbersStrArray.length > 1) {
            if (numbersStrArray[numbersStrArray.length - 1].equalsIgnoreCase("null")) {
                numLength = numbersStrArray.length - 1;
            } else {
                numLength = numbersStrArray.length;
            }
        } else {
            numLength = 1;
        }

        int[] numbers = new int[numLength];
        for (int i = 0; i < numLength; i++) {
            numbers[i] = Integer.parseInt(numbersStrArray[i]);
        }

        return constructListNode(numbers);
    }

测试代码如下:

    public static void main(String[] args) {
        System.out.println(printLinkedList(constructListNode("1->2->3->4->5->NULL ")));
        System.out.println(printLinkedList(constructListNode(" 1->2->3->4->5")));
        System.out.println(printLinkedList(constructListNode(" 1->2->3->4 ->5 -> null ")));
        System.out.println(printLinkedList(constructListNode("[1, 2, 3, 4 ,5]")));
        System.out.println(printLinkedList(constructListNode("[1, 2, 3, 4 , 5, null]")));
        System.out.println(printLinkedList(constructListNode("[1,  2, 3, 4 , 5, NULL]")));
    }

小结

上述代码集成在LinkedListUtils类中,包含:

  1. printLinkedList(ListNode head),打印一个链表的方法;
  2. 两个创建链表的方法: constructListNode(int[] numbers),由整型数组创建;constructListNode(String str),由字符串创建。

本文来源:如何根据数组或者字符串创建链表?

相关文章

  • 如何根据数组创建二叉树和打印二叉树?

    By Long Luo 之前的如何根据数组或者字符串创建链表?[http://www.longluo.me/blo...

  • 1.数据结构-单链表的基本操作

    文章内容: 1.根据数组array创建单链表 2.打印单链表 一.首先创建单链表的节点类Node: 二.根据数组a...

  • 单链表二-单链表的整表创建和删除

    单链表的整表创建:单链表的创建不能像顺序循序结构一样通过数组来创建,它的创建应该根据实际情况来,因为单链表的生成是...

  • 15 反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头。 思路1:创建辅助空间 创建一个数组。。翻转一下 或者创建一...

  • C++根据数组创建单向链表

    在刷牛客网试题的时候发现和leetcode不一样的地方,leetcode只需要写核心算法,而牛客网需要写完整程序。...

  • JS基础09-17

    数组面向对象创建 直接创建 数组嵌套 数组字符拼接 数组去重 字符串字符串翻转/字符串翻转---线分割数组,翻转,...

  • 数据结构有哪些

    数组、链表、哈希、队列、堆、栈、图、树、字符串

  • C语言之栈的实现

    以数组的方式创建栈 以链表的方式创建栈

  • Object-C 基本语法

    基本类型 创建对象 字符串 创建字符串 字符串长度 获取子串 字符串拼接 字符串是否相等 字符串替换 数组 数组是...

  • 必须掌握的基础

    数据结构 数组和字符串:快慢(前后)指针 链表:创建、插入、删除 树: 三种遍历递归与循环-前中后 层次遍历 特例...

网友评论

      本文标题:如何根据数组或者字符串创建链表?

      本文链接:https://www.haomeiwen.com/subject/qjzbhltx.html