【问题标题】:Insert a node at a specific position in a linked list JAVA在链表JAVA中的特定位置插入节点
【发布时间】:2022-10-18 05:33:45
【问题描述】:
public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
    if(llist == null) {
        llist =  new SinglyLinkedListNode(data);
        return llist;
    } else {
        for (int i = 0; i < position-1; i++) {
            llist = llist.next;
        }
        SinglyLinkedListNode temp = llist;
        llist.next = new SinglyLinkedListNode(data);
        llist = llist.next;
        llist.next = temp.next;         
        return llist;
    }
}

这是我在 LinkedList 中放置自定义索引节点的代码。但是hackerrank不接受我的代码。我的算法有什么问题?

【问题讨论】:

    标签: java algorithm linked-list singly-linked-list


    【解决方案1】:

    问题是你的代码总是返回新创建的节点,但你应该总是返回第一的列表中的节点,要么是你得到它时的样子,要么是位置为零的新节点。

    看什么:

    我不会提供更正后的代码,但会给你两个提示:

    通过在for 循环中将llist 向前移动,您将丢失对第一个节点的引用,因此请使用不同的变量来遍历列表。

    此外,您应该专门处理position 为0 的情况,因为这是唯一返回值不是原始llist 值,而是新节点的引用的情况,就像您在if 中一样堵塞。

    【讨论】:

      【解决方案2】:

      最简单的解决方案无需解释: 解决方案 :

      static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
              if (head == null) return null;
              SinglyLinkedListNode temp = new SinglyLinkedListNode(data);
              if (position == 0) {
                  temp.next = head;
                  return temp;
              }
              SinglyLinkedListNode p = head;
              for (int i = 0; i< position-1; i++) {
                  p = p.next;
              }
              SinglyLinkedListNode next = p.next;
              p.next = temp;
              temp.next = next;
              return head;
          }
      

      【讨论】:

        【解决方案3】:

        该问题要求您返回一个链表。当我们被要求返回一个链表时,实际上我们返回的是链表的第一个节点。

        因此,您的问题是代码脚本中的返回值不是链表的第一个节点。

        最简单的解决方案是将第一个节点保存在另一个变量中,并且
        完成插入操作后返回该变量。 例如:

        SinglyLinkedListNode dumyNode = llist;
        ......
        return dumyNode;
        

        【讨论】:

          【解决方案4】:

          假设给定正确的 Node 类,您可以尝试这种方法(没有索引冲突的情况):

          private Node find(int index) {
              Node curr = head;
              for (int i = 0; i < index; i++)
                  curr = curr.next;
          
              return curr;
          }   // end find()
          
          public Object get(int index) throws IndexOutOfBoundsException {
              if (index >= 0 && index < size) {
                  Node curr = find(index);
                  return curr.data;
              } else {
                  throw new IndexOutOfBoundsException();
              }   // end if - else
          }   // end get()
          
          public void add(Object data, int index) throws IndexOutOfBoundsException {
              if (index >= 0 && index < size + 1) {
                  if (index == 0)
                      head = new Node(data);
                  else {
                      Node prev = find(index - 1);
                      prev.next = new Node(data);
                  } // end if - else
                  size++;
              } else {
                  throw new IndexOutOfBoundsException();
              }   // end if - else
          }   // end add()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-02-08
            • 1970-01-01
            • 2012-05-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-23
            • 2014-02-11
            相关资源
            最近更新 更多