反转链表

时间:2012-05-17 17:48:00

标签: java linked-list

在不使用递归的情况下反转链接列表时出现问题。

我使用了这种方法,但是当我尝试在家中运行时,我无法打印链表的反向,即使该功能看起来很好它继续以与它相同的方式打印链表早些时候做过。

有人可以帮我理解这里有什么问题吗?

class link {
    int data;
    public link nextlink;

    link(int d1) {
        data = d1;
    }
}

class List{

    link head;
    link revhead;

    List(){
        head = null;
    }

    boolean isEmpty(link head) {
       return head==null;
    }

    void insert(int d1) {
        link templink = new link(d1);
        templink.nextlink = head;
        head = templink;
    }

    void printlist(){
        link head1 = head;
        while(!isEmpty(head1)) {
            System.out.print(head1.data + " ");
            head1 = head1.nextlink;
        }
        System.out.println();
    }

    void reverse() {
        link previous=null,temp=null;
        while(isEmpty(head)) {
            temp = head.nextlink;
            head.nextlink = previous;
            previous = head;
            head = temp;
        }
    }

}

public class LinkedList {

    public static void main(String[] args) {

        List list1 = new List();

        list1.insert(10);
        list1.insert(20);
        list1.insert(30);
        list1.insert(40);
        list1.insert(50);
        list1.printlist();
        list1.reverse();
        list1.printlist();
     }
}

5 个答案:

答案 0 :(得分:4)

您的代码中存在两个问题。一:你检查isEmpty(head)你应该检查的位置!isEmpty(head)。第二:当你解决第一个问题时,当循环终止时,'head'变为null。

修正以上两个问题的正确代码:

void reverse() {
    link previous = null, temp = null;
    while (!isEmpty(head)) {
        temp = head.nextlink;
        head.nextlink = previous;
        previous = head;
        if (temp == null) {
           break;
        }
        head = temp;
    }

}

答案 1 :(得分:0)

检查你的情况:  while(isEmpty(head))

你忘记添加“!”你的意思是不是空的......做

答案 2 :(得分:0)

考虑在打印链接时递归方法的作用。它实际上并没有做任何事情,直到它拥有它们(即它将每个调用放在堆栈上,然后当达到基本情况时,它从堆栈中弹出元素)。

您有哪些非堆栈结构可用于存储列表的反向,然后让您将其打印出来?

答案 3 :(得分:0)

用于在没有递归的情况下反转链接列表的更正代码。

class Link {

    int data;
    public Link nextLink;

    Link(int d1) {
        data = d1;
    }
}

class List {

    Link head;
    Link revhead;

    List() {
        head = null;
    }

    boolean isEmpty(Link head) {
        return head == null;
    }

    void insert(int d1) {
        Link tempLink = new Link(d1);
        tempLink.nextLink = head;
        head = tempLink;
    }

    void printlist() {
        Link head1 = head;
        while (!isEmpty(head1)) {
            System.out.print(head1.data + " ");
            head1 = head1.nextLink;
        }
        System.out.println();
    }

    void reverse() {
        Link previous = null, temp = null;
        while (!isEmpty(head)) {
            temp = head.nextLink;
            head.nextLink = previous;
            previous = head;
            head = temp;
        }

        head = previous;
    }
}

public class Main {

    public static void main(String[] args) {

        List list1 = new List();

        list1.insert(10);
        list1.insert(20);
        list1.insert(30);
        list1.insert(40);
        list1.insert(50);
        list1.printlist();
        list1.reverse();
        list1.printlist();
    }
}

答案 4 :(得分:-1)

class Node {
    Node next;
    int value;

    public Node() {
    }

    public Node(Node next, int value) {
        super();
        this.next = next;
        this.value = value;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

}

public class Linkedlist {
    private Node head = null;

    public Linkedlist(Node head) {
        this.head = head;
    }

    public void iterate() {
        Node current = head;
        while (current != null) {
            System.out.println(current.getValue());
            current = current.getNext();
        }
    }

    public void reverse() {
        Node current = head;
        Node prev = null;
        while (current != null) {
            Node temp = current.next;
            current.next = prev;
            prev = current;
            current = temp;
        }
        head = prev;
    }

    public static void main(String[] args) {
        Node n = new Node(null, 10);
        Node n1 = new Node(n, 20);
        Node n2 = new Node(n1, 30);
        Node n3 = new Node(n2, 40);
        Node n4 = new Node(n3, 50);
        Node n5 = new Node(n4, 60);
        Linkedlist linkedlist = new Linkedlist(n5);
        linkedlist.iterate();
        linkedlist.reverse();
        System.out.println("------------------REVERSED---------------------");
        linkedlist.iterate();

    }
}