【问题标题】:Flatten a Linked List in Java在 Java 中展平链表
【发布时间】:2015-05-10 22:37:24
【问题描述】:

我正在尝试展平多级链接列表。 给定一个链表,其中每个节点都表示一个链表并包含其类型的两个指针: (i) 指向主列表中下一个节点的指针(我们在下面的代码中称其为“右”指针) (ii) 指向此节点为头的链表的指针(我们在下面的代码中将其称为“向下”指针)。 所有链表都已排序

   5 -> 10 -> 19 -> 28
   |    |     |     |
   V    V     V     V
   7    20    22    35
   |          |     |
   V          V     V
   8          50    40
   |                |
   V                V
   30               45

5 7 8 10 19 20 22 28 30 35 40 45 50

下面是我的 Java 代码:

public class FlattenAList {
public static MultiNode<Integer> end = new MultiNode<Integer>(0);
public static MultiNode<Integer> result = end;

public static MultiNode flatten(MultiNode head) {
    if (head == null || head.right == null)
        return head;
    MultiNode<Integer> tmp = head;

    while (tmp != null) {
        merge(tmp, result);
        tmp = tmp.right;
    }
    return result;
}

public static void merge(MultiNode<Integer> a, MultiNode<Integer> b) {
    if (a == null) {
        end.down = b;
        end = end.down;
        return;
    }
    if (b == null) {
        end.down = a;
        end = end.down;
        return;
    }

    if (a.data <= b.data) {
        end.down = a;
        end = end.down;
        merge(a.down, b);
    } else {
        end.down = b;
        end = end.down;
        merge(a, b.down);
    }
}
}

合并函数有问题,我收到 java.lang.StackOverflowError 在 java.lang.Integer.intValue(Unknown Source)

if (a.data

【问题讨论】:

  • 你扁平化的逻辑是什么?形容它。例如,输入中有一个20,输出中有两个。您的输入中有一个 28,而您的输出中没有它。
  • 那么解释呢?目前,此问题将作为“为什么此代码不起作用”类型的问题关闭。您需要学习使用调试器并创建 SSCCE。这里的代码太多了。并且没有解释它应该做什么。
  • 它应该展平排序列表列,请查看上面的描述
  • 所以你想做一个内联展平?
  • 那么end 来自哪里?

标签: java data-structures linked-list


【解决方案1】:

这是一种快速简便的方法,可以根据您的需要进行操作。

package example;

public class FlattenAList {

    private static MultiNode<Integer> merge(MultiNode<Integer> a, MultiNode<Integer> b) {
        MultiNode<Integer> head = new MultiNode<Integer>();
        MultiNode<Integer> temp = head;
        while (a != null && b != null) {
            if (a.data < b.data) {
                temp.down = a;
                temp = temp.down;
                a = a.down;
            } else if (b.data < a.data) {
                temp.down = b;
                temp = temp.down;
                b = b.down;
            }
        }

        temp.down = (a == null) ? b : a;
        return head.down;
    }

    static class MultiNode<T> {
        T data;
        MultiNode<T> right;
        MultiNode<T> down;

        public MultiNode(T data) {
            this.data = data;
        }

        public MultiNode() {

        }
    }

    public static MultiNode<Integer> flatten(MultiNode<Integer> root) {

        MultiNode<Integer> temp = root;
        MultiNode<Integer> result = null;
        while (temp != null) {
            result = merge(temp, result);
            temp = temp.right;
        }
        return result;
    }

    public static void print(MultiNode<Integer> start) {
        while (start != null) {
            System.out.print(start.data+" ");
            start = start.down;
        }
    }

    public static void main(String[] args) {
        MultiNode<Integer> start = new MultiNode<Integer>(5);
        start.right = new MultiNode<Integer>(10);
        start.right.right = new MultiNode<Integer>(19);
        start.right.right.right = new MultiNode<Integer>(28);

        start.down = new MultiNode<Integer>(7);
        start.down.down = new MultiNode<Integer>(8);
        start.down.down.down = new MultiNode<Integer>(30);

        start.right.down = new MultiNode<Integer>(20);

        start.right.right.down = new MultiNode<Integer>(22);
        start.right.right.down.down = new MultiNode<Integer>(50);

        start.right.right.right.down = new MultiNode<Integer>(35);
        start.right.right.right.down.down = new MultiNode<Integer>(40);
        start.right.right.right.down.down.down = new MultiNode<Integer>(45);
        // Node result = flatten(start);
        MultiNode<Integer> result = flatten(start);
        print(result);
    }
}

输出:

5 7 8 10 19 20 22 28 30 35 40 45 50 

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2017-10-01
    • 2021-12-27
    相关资源
    最近更新 更多