【问题标题】:Singly Linked List - Put last node after first node单链表 - 将最后一个节点放在第一个节点之后
【发布时间】:2018-05-28 14:11:09
【问题描述】:

假设我有一个 1->2->3->4->5 的列表。我想把最后一个节点放在第一个节点之后,所以它就像 1->5->2->3->4。这是我的代码,但它不起作用

public void Manipulate(){
        Node curr = head;
        Node next = null;
        Node last = head;

        while(last.next != null){
            last = last.next;
        }

        next = curr.next;
        last.next = next;
        curr.next = next.next;

    }


 public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        SinglyLinkedList lista = new SinglyLinkedList();

        int a = sc.nextInt();
        int b = sc.nextInt();
        lista.addFirst(a);
        lista.insertAfter(a, b);

        for(int i = 0; i < 2; i ++){
            int c = b;
            b = sc.nextInt();
            lista.insertAfter(c, b);
        }

        lista.addLast(34);
        lista.addLast(55);
        lista.addLast("Ginger");

        lista.Manipulate();
        System.out.println(lista);

    }

【问题讨论】:

  • 什么不起作用?
  • 抛出异常 - 线程“main”中的异常 java.lang.OutOfMemoryError: Java heap space
  • 又是怎么调用的?
  • 提供了我的主要方法,你可以从那里检查出来
  • 如果您不提供代码,我们应该如何知道addFirst()、insertAfter() 等会发生什么?您是否调试过代码并检查会发生什么? OutOfMemoryError 意味着您正在无限循环中添加元素。

标签: java data-structures singly-linked-list


【解决方案1】:

你已经得到了一些改进建议,所以这里稍微解释一下你到底做错了什么。

您的代码的结果(原样)将是:

1->3->4->5->2->3->4->5->2->3->4->5->2->3->4->5- >2->3->4->5->2->3->4->5->2->3->4->5->2->3->->4->5- >2->3->4->5->2->3->->4->5->2->3->4->5->2->3->4->5- >2->3->4->5->2->3->->4->5->2->3->4->5->2->3->4->5- >2->3->4->5->2->3->4->5->2->3->4->5->2->3->...(永远持续下去)

这就是为什么您在尝试打印无限野兽时内存不足的原因。

第一个错误:您实际上需要确保将成为新最后一个元素的元素不再具有您以前的最后一个元素 next。

第二个错误:您的 head 元素应该将前最后一个元素作为 next,而不是其前 next 元素的 next 元素。

【讨论】:

    【解决方案2】:
        // Edge case: list has zero or 1 node:
        if(head == null || head.next == null) {
           return;
        }        
        Node prev = null;
        Node last = head;
    
        while(last.next != null){
            prev = last;
            last = last.next;
        }
    
        Node tmp = head.next;
        head.next = last;
        last.next = tmp;
    
        // Prevent loop by setting the next of the new last to null
        prev.next = null;
    

    【讨论】:

      【解决方案3】:
      public void Manipulate() {
          Node penultimate = null;
          Node last = head;
      
          while(last.next != null){
              penultimate = last;
              last = last.next;
          }
          if (penultimate != null){ // closes the list
              penultimate.next = null;
          }
          if (last != head) { // move last element to second place
              last.next = head.next;
              head.next = last;
          }
      }
      

      【讨论】:

      • 请编辑您的帖子,使其不仅仅是代码。为什么这解决了 OP 的问题?
      • everLast.next = null;关闭列表,因此在浏览列表时不再有任何 OP。
      • Tx 为 -1。对于工作代码。这些家伙给出了不完整的错误代码。我更正了我可以看到的错误。我必须解释???他应该看看我的代码并尝试理解他的错误。
      猜你喜欢
      • 1970-01-01
      • 2019-06-08
      • 2016-06-11
      • 2022-08-17
      • 2010-11-10
      • 1970-01-01
      • 2013-07-26
      • 2013-03-25
      • 2014-05-22
      相关资源
      最近更新 更多