首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反转未按预期工作的链表

问反转未按预期工作的链表
EN

Stack Overflow用户
提问于 2021-02-28 23:16:32
回答 1查看 35关注 0票数 0

我目前正在尝试反转一个链表,我有一个意外的问题,它不能正确打印。当我尝试访问链表的值时,我得到一个错误。如果有专家的眼光来看看我错过了什么,我会非常感激的。提前谢谢。

代码语言:javascript
复制
public class SinglyLinkedList implements Cloneable, Iterable, List {
    //---------------- nested Node class ----------------

    /**
     * Node of a singly linked list, which stores a reference to its
     * element and to the subsequent node in the list (or null if this
     * is the last node).
     */
    private static class Node {
        private E data;  // reference to the element stored at this node

        private Node nextNode; 

        
        //methods for accessing variables

        public Node getNextNode() { return nextNode; }

        public E getData() { return data; }

        // Modifier methods
        public void setNext(Node n) { nextNode = n; }
        public void setData(E n) { data = n; }
        public Node(E e, Node n) {
            nextNode = n;
            data = e;
        }

        // reference to the subsequent node in the list// TODO
    } //----------- end of nested Node class -----------

    // instance variables of the SinglyLinkedList
    private int size = 0; // number of nodes in the list
    private Node head = null; // head node of the list (or null if empty)

    public SinglyLinkedList() {
    }              // constructs an initially empty list

    // access methods

    /**
     * Returns the number of elements in the linked list.
     *
     * @return number of elements in the linked list
     */
public void addFirst(E e) {
        head = new Node(e, head); // create the new node and link new node
        size++;
    }
    /**
     * Produces a string representation of the contents of the list.
     * This exists for debugging purposes only.
     */
    public String toString() {
        StringBuilder temporaryString = new StringBuilder();
        temporaryString.append("[");
        for(Iterator iterator = iterator(); iterator.hasNext();){
            temporaryString.append(iterator.next()).append(", ");
        }
        temporaryString.deleteCharAt(temporaryString.length() - 1);
        temporaryString.deleteCharAt(temporaryString.length() - 1);
        temporaryString.append("]");
        return temporaryString.toString();
    }

    private class SinglyLinkedListIterator implements Iterator {
        private Node current;
        public SinglyLinkedListIterator() {
            current = head;
        }
        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public E next() {
            if(!hasNext()) throw new RuntimeException("No such element");

            E res = current.getData();
            current = current.getNextNode();
            return res;
        }
    }

    public Iterator iterator() {
        return new SinglyLinkedListIterator();
    }

反向链表方法:

代码语言:javascript
复制
public Node reverseLinkedList(SinglyLinkedList sll) {
        Node previous = null;
        Node current = sll.head;
        while (current != null) {
            Node nextElement = current.getNextNode();
            current.setNext(previous);
            previous = current;
            current = nextElement;
        }
        return previous;
    }

主要方法:

代码语言:javascript
复制
public static void main(String[] args) {
        SinglyLinkedList sll2 = new SinglyLinkedList();
        sll2.addFirst(1);
        sll2.addFirst(2);
        sll2.addFirst(3);

System.out.println(sll2.toString());
        sll2.reverseLinkedList(sll2);
System.out.println(sll2.toString());
    }

输出:

代码语言:javascript
复制
[3, 2, 1]
//i should expect to get 1,2,3
[3]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-28 23:36:38

就像你一样

变异

(“重新连接”)方法中给定的链表。

函数时,实际上并不是在生成新的链表。所以让它返回一些东西实际上是矛盾的。要么它应该返回一个全新的链表而不改变给定的链表,要么它应该改变给定的链表而不返回任何东西。

来自您的

我看到的代码实际上是你想要的

变异

给定的链表,因为您不使用返回值并根据

..。因此,将您的函数设置为

一,然后删除

语句。

现在的核心问题是您永远不会更改

成员:它仍然引用以前是列表中第一个节点的内容,但现在是最后一个节点。您应该将该节点分配给

这之前是尾节点。

因此,函数中的最后一条语句应该是:

代码语言:javascript
复制
sll.head = previous;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66410553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档