如何将项目插入列表的前面?

How can I insert an Item to the front of the List?

我有一个名为“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());
    }
}

这是因为当它是头部时,你永远不会将它连接到列表的其余部分。你需要做的就是在前一个节点为空时添加一行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;

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

如果索引 == 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++;
                }
            }
        }
    }