обратный односвязный список Java? - PullRequest
0 голосов
/ 19 мая 2018

У меня проблема, я не могу запустить этот метод, я хочу изменить узлы в одном Linkedlist. Я говорю все сообщения из Stackoverflow об обратном, но они отличаются от моего кода.Вот мой код

public node reverse(node head) {
    node p,q;
    if(head==null) {
        return head;
    }
    p=head;
    q=p.next;

    if(q==null) {
        return p;
    }
    q=reverse(q);
    p.next.next=p;
    p.next=null;
    return q;

}public void printList(){
    node currentNode = head;
    while(currentNode != null){
        System.out.print(currentNode.data);
        currentNode=currentNode.next;
    }
}

Основной класс

public class main {

public static void main(String[] args) {
    linkedlist obj = new linkedlist();

    obj.insertFirst(1);
    obj.insertFirst(2);
    obj.insertFirst(3);
    obj.insertFirst(4);
    obj.insertFirst(5);
    obj.reverse(head);  
    obj.printList();

Пожалуйста, дайте мне решение по этому коду.

Ответы [ 2 ]

0 голосов
/ 19 мая 2018

Ваш код не имеет большого смысла, и трудно сделать это правильно.Я бы рекомендовал сначала перейти к более простому и лаконичному подходу, как этот.

public node reverse(node head) {
    if(head == null || head->next == mull) {
        return head;      
    }
    node current = head;
    node newHead = null;
    node previous = null;
    while(current != null) {
        node nxt = current.next;
        current.next = previous;
        newHead = current;
        previous = current;
        current = nxt;
    }
    return newHead;
}
0 голосов
/ 19 мая 2018

Перевернутый связанный список:

class LinkedList {

    static Node head;

    static class Node {

        int data;
        Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    /* Function to reverse the linked list */
    Node reverse(Node node) {
        Node prev = null;
        Node current = node;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }

    // prints content of double linked list
    void printList(Node node) {
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(85);
        list.head.next = new Node(15);
        list.head.next.next = new Node(4);
        list.head.next.next.next = new Node(20);

        System.out.println("Given Linked list");
        list.printList(head);
        head = list.reverse(head);
        System.out.println("");
        System.out.println("Reversed linked list ");
        list.printList(head);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...