简体   繁体   English

链表反向并将新节点插入反向链表

[英]Linked List reversing and inserting new node to the reversed linked list

I'm learning DSA, and I was trying to implement the reverse operation of a linked list, but apparently the create method that I've written is not working after executing the reversal operation.我正在学习DSA,我正在尝试实现链表的反向操作,但显然我编写的create方法在执行reversal操作后不起作用。

Here is the code:这是代码:

import java.util.*;

public class reverse {
    Scanner sc = new Scanner(System.in);

    static class Node {
        int data;
        Node next;

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

    Node head = null;
    Node tail = null;

    public void create() {
        System.out.println("Enter data");
        int value = sc.nextInt();
        Node newnode = new Node(value);
        if (head == null) {
            head = newnode;
            tail = newnode;
        } else {
            tail.next = newnode;
            tail = newnode;
        }
    }

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

    public void display() {
        Node temp = head;
        while (temp.next != null) {
            System.out.println(temp.data);
            temp = temp.next;
        }
        System.out.println(temp.data);
    }

    public static void main(String[] args) {
        int d = 0, a;
        Scanner sc = new Scanner(System.in);
        reverse obj = new reverse();
        do {
            System.out.println("1.Create\n2.Reverse\n3.Display");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    obj.create();
                    break;
                case 2:
                    obj.reversal2();
                    break;
                case 3:
                    obj.display();
                    break;
                default:
                    System.out.println("Enter a valid number!");
                    break;
            }
            System.out.println("1-Continue,0-Exit");
            a = sc.nextInt();
            if (a == 1) {
                d = 1;
            } else if (a != 1 && a != 0) {
                System.out.println("Enter a valid number");
            } else {
                System.out.println("Code Terminated!!");
                d = 0;
            }
        } while (d == 1 && d != 0);
    }
}

This is the output:这是输出:

1.Create 
2.Reverse 
3.Display
1

Enter data
1

1-Continue,0-Exit
1

1.Create
2.Reverse
3.Display

1

Enter data
2

1-Continue,0-Exit
1

1.Create
2.Reverse
3.Display
1

Enter data
3

1-Continue,0-Exit
1

1.Create
2.Reverse
3.Display
2

1-Continue,0-Exit
1

1.Create
2.Reverse
3.Display
3

Numbers are:
3
2
1

1-Continue,0-Exit
1

After reversing the linked list.反转链表后。

1.Create
2.Reverse
3.Display
1

Enter data
4

1-Continue,0-Exit
1

1.Create
2.Reverse
3.Display
3

Numbers are:
3
4

1-Continue,0-Exit
0

I am not able to figure out what's wrong.我无法弄清楚出了什么问题。

In your reversal method you should keep 3 temporary nodes:在您的reversal方法中,您应该保留 3 个临时节点:

  • one to iterate the list (as you're doing).一个迭代列表(就像你正在做的那样)。
  • one maintaining the head of the reversed list.一个保持反向列表的头部。
  • one to create a new node for the reversed list at each iteration of the list traversal.在列表遍历的每次迭代中为反向列表创建一个新节点。

During each loop, the new node for the reversed list should contain the info of the currently visited node, then you could attach to this node the reversed list (so the last node created points to the first nodes visited) and finally updated the head of the reversed list with the new node created.在每个循环中,反向列表的新节点应包含当前访问节点的信息,然后您可以将反向列表附加到该节点(因此创建的最后一个节点指向访问的第一个节点),最后更新创建新节点的反向列表。

Error Explanation错误说明

The reason why you couldn't add any further elements after reversing your list is because your tail node wasn't being updated.反转列表后无法添加任何其他元素的原因是您的tail节点未更新。 In fact, in your version tail was left referencing a non-existing node (the old list is gone once you update head with the new reversed list).事实上,在您的版本中, tail被留下引用一个不存在的节点(一旦您使用新的反向列表更新head ,旧列表就消失了)。 In your while loop you need to set tail to the first new node created, as this will become the last node of your reversed list at the end of the loop, ie your tail.在您的while循环中,您需要将tail设置为创建的第一个新节点,因为这将成为循环结束时反向列表的最后一个节点,即您的tail。

public void reversal() {
    //Temp node to iterate the list
    Node temp = head;
    
    //Head of the reversed list
    Node headRev = null;
    
    //Temporary node to create at each iteration a new node for the reversed list
    Node newNode;

    //Resetting the tail
    tail = null;

    //Iterating the list
    while (temp != null) {
        
        //Creating a new node with the info of the current node
        newNode = new Node(temp.data);

        //Setting the tail of the new reversed list with the first node created
        tail = tail == null ? newNode : tail;
        
        //Appending to the new node the rest of the reversed list
        newNode.next = headRev;
        
        //Moving the head of the reversed list to the new node just created
        headRev = newNode;
        
        //Moving the iteration of the list to the next node
        temp = temp.next;
    }
    
    //Updating the head node of the list with the head of the reversed list
    head = headRev;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM