在java中反转单链表

时间:2015-01-05 07:07:36

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

我在java中从头开始制作了一个单独的链表。代码如下:

public class SingleLinkedList<Item>
{
private Node head;
private int size;

private class Node
{
    Item data;
    Node next;

    public Node(Item data)
    {
        this.data = data;
        this.next = null;
    }

    public Node(Item data, Node next)
    {
        this.data = data;
        this.next = next;
    }
    //Getters and setters
    public Item getData()
    {
        return data;
    }
    public void setData(Item data)
    {
        this.data = data;
    }
    public Node getNext()
    {
        return next;
    }
    public void setNext(Node next)
    {
        this.next = next;
    }

}

public SingleLinkedList()
{
    head = new Node(null);
    size = 0;
}

public void add(Item data)
{
    Node temp = new Node(data);
    Node current = head;

    while(current.getNext() != null)
    {
        current = current.getNext();
    }

    current.setNext(temp);
    size++;
}

public void add(Item data, int index)
{
    Node temp = new Node(data);
    Node current = head;

    for(int i=0; i<index && current.getNext() != null; i++)
    {
        current = current.getNext();
    }

    temp.setNext(current.getNext());
    current.setNext(temp);
    size++;
}

public Item get(int index)
{
    if(index <= 0)
    {
        return null;
    }

    Node current = head;

    for(int i=1; i<index; i++)
    {
        if(current.getNext() == null)
        {
            return null;
        }

        current = current.getNext();
    }

    return current.getData();
}

public boolean remove(int index)
{
    if(index < 1 || index > size())
    {
        return false;
    }

    Node current = head;
    for(int i=1; i<index; i++)
    {
        if(current.getNext() == null)
        {
            return false;
        }

        current = current.getNext();
    }

    current.setNext(current.getNext().getNext());
    size--;
    return true;
}

public String toString()
{
    Node current = head.getNext();
    String output = "";
    while(current != null)
    {
        output+=current.getData().toString()+"  ";
        current = current.getNext();
    }

    return output;
}

public int size()
{
    return size;
}

public void reverse()
{
    Node current = head;
    Node prevNode = null;
    Node nextNode;

    while(current!=null)
    {
        nextNode = current.getNext();
        current.setNext(prevNode);
        prevNode = current;
        current = nextNode;
        System.out.println(prevNode.getData());
    }

    head = prevNode;

}

}

如您所见,我只在课堂上添加了反向功能。

但是当我尝试使用该类时,在我尝试反转它之后它给出了NullPointerException。

为了检查功能,我使用了另一个名为TEST的类。代码如下:

public class TEST
{

    public static void main(String[] args)
    {
        SingleLinkedList<Integer> list = new SingleLinkedList<Integer>();

        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        System.out.println(list.toString());

        list.reverse();
        System.out.println(list.toString());

    }
}

输出如下:

1  2  3  4  5  
null
1
2
3
4
5
Exception in thread "main" java.lang.NullPointerException
    at SingleLinkedList.toString(SingleLinkedList.java:129)
    at TEST.main(TEST.java:20)

我试图打印prevNode的值来检查它是否没有取值......但确实如此。 怎么办?

3 个答案:

答案 0 :(得分:0)

 while(current!=null)

这是你的问题。当您点击最后一个节点时,“下一个”节点你得到的节点实际上是空的。

尝试将其更改为

while(current!=null&&current.getNext()!=null)

编辑:实际上不确定解决方案是否有效。尝试在循环结束时设置条件:

if(current.getNext()==null)
    break;

编辑(再次:/):

好的抱歉,我没有直接思考。

将最终的if语句更改为:

if(current.getNext()==null){
    current.setNext(prevNode);
    break;
}

实际的nullpointer位于toString中。这是你做的:

将while条件更改为

while(current != null&&current.getData()!=null)

因为否则如果当前指向null,则会出现异常。

那令人筋疲力尽。

答案 1 :(得分:0)

实际上,你的反向方法看起来很好。

问题是你的toString()方法。
创建新列表时,将创建一个数据为空的初始元素 您的toString方法会跳过第一个元素,因此只要您不反转列表,它就可以正常工作 但是,当您反转列表时,该null元素将成为最后一个元素,当您在output+=current.getData().toString()+" ";为空时为最后一个元素调用current.getData()时,您将获得NullPointerException

您有几种选择:

  1. 你的反向方法可以保持初始的null元素(即反转列表的其余部分,但保持头部相同)。这样toString可以保持不变。
  2. 消除初始的null元素。然后你的toString方法不必跳过任何东西。
  3. 首先保留null元素:

    public void reverse()
    {
        Node current = head.getNext();
        Node prevNode = null;
        Node nextNode;
    
        while(current!=null)
        {
            nextNode = current.getNext();
            current.setNext(prevNode);
            prevNode = current;
            current = nextNode;
            System.out.println(prevNode.getData());
        }
    
        head.setNext(prevNode);
    
    }
    

答案 2 :(得分:0)

问题在于你的SingleLinkedList.java toString()方法

尝试下面它工作正常

    public String toString() {
        Node current = head;
        String output = "";
        while (current != null) {
//            output += current.getData().toString() + "  ";
            output += String.valueOf(current.getData()) + "  ";
            current = current.getNext();
        }

        return output;
    }