【问题标题】:How can I insert an Item to the front of the List?如何将项目插入列表的前面?
【发布时间】:2021-10-21 02:08:57
【问题描述】:

我有一个名为“public void insertAt(int index, int item)”的方法。此方法旨在“在位置索引处插入一个项目,将索引传递给该方法”我在下面有此方法的代码。当我在索引处插入一个项目时,它会起作用,除非它是列表中的第一个项目。当我尝试在列表的开头插入一个项目时,没有任何反应。例如,如果我有一个列表:“[9, 8, 15, 7, 5, 15, 19, 6, 19, 2]”并且我想在第一个节点中插入数字“90”,它应该看起来像[90, 9, 8, 15, 7, 5, 15, 19, 6, 19, 2] 但我得到的是“[9, 8, 15, 7, 5, 15, 19, 6, 19, 2]” .如何在我的代码中解决此问题,以便如果我要在头部插入一个项目,它会将我想要插入的项目移动到头部,并将所有其他项目移到列表中?

import java.util.Random;

public class LinkedListOfInts {
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

        public Node(int value, Node nextNode) {
            this.value = value;
            this.nextNode = nextNode;
        }

    }

    public LinkedListOfInts(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i++)
            this.addToFront(random.nextInt(high - low) + low);
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    public void insertAt(int index, int item) {
        Node temp = head;
        Node prev = null;
        int i = 0;
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
            if (index == i) {
                Node newItem = new Node(item, null);
                if (prev != null) {
                    prev.nextNode = newItem;
                }
                newItem.nextNode = temp;
            }
            if (temp.nextNode != null) {
                prev = temp;
                temp = temp.nextNode;
                i++;
            }
        }
    }

    public String toString() {
        String result = "";
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
            if (!result.isEmpty()) {
                result += ", ";
            }
            result += ptr.value;
        }
        return "[" + result + "]";
    }

    public static void main(String[] args) {
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        System.out.println(list.toString());
        list.insertAt(0, 27);
        System.out.println(list.toString());
    }
}

【问题讨论】:

标签: java linked-list nodes


【解决方案1】:

这是因为当它是头部时,你永远不会将它连接到列表的其余部分。您需要做的就是在前一个 Node 为 null 时添加一行 else 语句,就像这样

       for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
               if (index == i) {
                    Node newItem = new Node(item, null);
                    if (prev != null) {
                         prev.nextNode = newItem;
                    } else {
                         head = newItem;
                    }
                    newItem.nextNode = temp;
                    break;

(我为了性能而中断,插入新节点后无需遍历其余值)

【讨论】:

    【解决方案2】:

    如果 index == 0,您可以添加条件,然后只需从列表中添加一个新节点。

    这是你的功能

    public void insertAt(int index, int item) {
            if (index == 0) {
                Node node = new Node(item, null);
                node.nextNode = head;
                head = node;
            }
            else {
                Node temp = head;
                Node prev = null;
                int i = 0;
                for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
                    if (index == i) {
                        Node newItem = new Node(item, null);
                        if (prev != null) {
                            prev.nextNode = newItem;
                        }
                        newItem.nextNode = temp;
                    }
                    if (temp.nextNode != null) {
                        prev = temp;
                        temp = temp.nextNode;
                        i++;
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      相关资源
      最近更新 更多