如何使用自定义方法在链表的第0个位置插入节点?

huangapple go评论76阅读模式
英文:

How do I insert a node in linked list at 0th position using a custom method?

问题

我已经创建了一个方法,在第N个索引处插入一个节点到链表中。对于除了第0个位置之外的所有位置,该代码都正常工作。请查看下面的代码,并告诉我哪里有问题。

以下代码有两个插入方法:

  1. insert(int data): 用于添加一个新节点。
  2. insertAtNthPositon(int position, int data): 用于在第N个位置插入元素。
public class LinkedList {
    Node head;

    static class Node {
        int data;
        Node next;
    }

    public void insert(int data) {
        Node new_node = new Node();
        new_node.data = data;
        new_node.next = null;

        if (head == null) {
            head = new_node;
        } else {
            Node n = head;
            while (n.next != null) {
                n = n.next;
            }
            n.next = new_node;
        }
    }

    public void insertAtNthPositon(int position, int data) {
        Node new_node = new Node();
        new_node.data = data;
        Node current_node = head;

        int i = 0;
        if (position == 0) {
            new_node.next = current_node;
            head = new_node;
        } else {
            while (i < position - 1) {
                current_node = current_node.next;
                i++;
            }
            new_node.next = current_node.next;
            current_node.next = new_node;
        }
    }

    public static void main(String[] arg) {
        LinkedList ls = new LinkedList();
        ls.insert(6);
        ls.insert(7);
        ls.insert(9);
        ls.insertAtNthPositon(1, 100);
        ls.show();
    }

    private void show() {
        Node current = head;
        StringBuilder sb = new StringBuilder();
        do {
            sb.append(current.data);
            sb.append("->");
            current = current.next;
        } while (current != null);
        sb.append("null");
        System.out.println(sb.toString());
    }
}

输出:

6->100->7->9->null

英文:

I have created a method to insert a node in LinkedList at Nth index. The code works fine for all the positions except 0th position. Please review my code below and let me know what's wrong.
The below code has 2 insert methods:

  1. insert(int data): This used to add a new node.
  2. insertAtNthPositon(int position, int data): This helps to insert the element at Nth position.
public class LinkedList {
Node head;
static class Node {
int data;
Node next;
}
public void insert(int data) {
Node new_node = new Node();
new_node.data = data;
new_node.next = null;
if (head == null) {
head = new_node;
} else {
Node n = head;
while (n.next != null) {
n = n.next;
}
n.next = new_node;
}
}
public void insertAtNthPositon(int position, int data) {
Node new_node = new Node();
new_node.data = data;
Node current_node = head;
int i = 0;
if (position == 0) {
new_node.next = current_node.next;
head = new_node;
current_node = current_node.next;
}
while (i &lt; position - 1) {
current_node = current_node.next;
i++;
}
new_node.next = current_node.next;
current_node.next = new_node;
}
public static void main(String[] arg) {
LinkedList ls = new LinkedList();
ls.insert(6);
ls.insert(7);
ls.insert(9);
ls.insertAtNthPositon(1, 100);
ls.show();
}
private void show() {
Node current = head;
StringBuilder sb = new StringBuilder();
do {
sb.append(current.data);
sb.append(&quot;-&gt;&quot;);
current = current.next;
} while (current != null);
sb.append(&quot;null&quot;);
System.out.println(sb.toString());
}
}

Output:
>6->100->7->9->null

答案1

得分: 0

正如我所建议的:你需要设置 [在位置:如果(position == 0)] 新节点的 new_node.nextcurrent_node,而不是 current_node.next -> 这就是为什么你总是从索引 1 开始而不是 0。

public void insertAtNthPositon(int position, int data){
    Node new_node = new Node();
    new_node.data = data;
    Node current_node = head;

    int i = 0;
    if (position == 0){
        new_node.next = current_node; // 而不是 current_node.next
        head = new_node;
        current_node = current_node.next;
    }
    while(i < position - 1){
        current_node = current_node.next;
        i++;
    }
    new_node.next = current_node.next;
    current_node.next = new_node;
}

顺便说一下,ls.insertAtNthPositon(1, 100); 是在索引 1 处插入 100,如果你想要将 100 插入为第一个元素,应该使用 ls.insertAtNthPositon(0, 100);

英文:

Just as I suggested: you need to set [at: if (position == 0)] the new_node.next to current_node and not current_node.next -> this is why you always start from index 1 and not 0.

   public void insertAtNthPositon(int position, int data){
Node new_node = new Node();
new_node.data =data;
Node current_node = head;
int i = 0;
if (position == 0){
**new_node.next = current_node;** &lt;instead of current_node.next&gt;
head = new_node;
current_node = current_node.next;
}
while(i &lt; position - 1){
current_node = current_node.next;
i++;
}
new_node.next = current_node.next;
current_node.next = new_node;
}

BTW- ls.insertAtNthPositon(1, 100); insert 100 at index 1, you want to do ls.insertAtNthPositon(0, 100); in order for '100' to be the first.

huangapple
  • 本文由 发表于 2020年9月16日 03:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63908415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定