欢迎访问宙启技术站
智能推送

Java函数实现链表的反转

发布时间:2023-06-29 20:00:44

链表是一种常见的数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。

要实现链表的反转,我们可以使用迭代或递归的方式。

迭代方法:

1. 定义两个指针,一个指向当前节点,一个指向前一个节点,并初始化为null。

2. 在循环中,将当前节点的下一个节点保存到一个临时变量中。

3. 将当前节点的指针指向前一个节点。

4. 将前一个节点的指针指向当前节点。

5. 将当前节点的指针指向临时变量,即将指针向后移动一位。

6. 重复以上步骤,直到当前节点为null,完成链表的反转。

以下是Java代码的实现:

class Node {
    int data;
    Node next;

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

class LinkedList {
    Node head;

    public LinkedList() {
        this.head = null;
    }

    // 方法:反转链表
    public void reverse() {
        Node current = head;
        Node previous = null;
        
        while (current != null) {
            Node next = current.next; // 保存当前节点的下一个节点

            current.next = previous; // 将当前节点的指针指向前一个节点

            previous = current; // 将前一个节点指向当前节点
            current = next; // 将指针向后移动一位
        }
        
        head = previous; // 更新头节点
    }

    // 方法:添加节点到链表
    public void addNode(int data) {
        Node newNode = new Node(data);
        
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    // 方法:打印链表
    public void printList() {
        Node current = head;
        
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();

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

        System.out.println("原链表:");
        list.printList();

        list.reverse();

        System.out.println("反转后的链表:");
        list.printList();
    }
}

以上代码中,我们定义了一个Node类,表示节点,和一个LinkedList类,表示链表。LinkedList类有三个方法:reverse()用于反转链表,addNode()用于添加节点到链表,printList()用于打印链表。在main()方法中,我们创建了一个链表对象,添加了几个节点并打印原链表,然后调用reverse()方法反转链表并打印反转后的链表。

递归方法:

1. 递归地将下一个节点作为参数传递给反转方法。

2. 在反转方法中,如果当前节点为null或下一个节点为null,则返回当前节点。

3. 否则,递归调用反转方法,将下一个节点作为参数,并保存返回值。

4. 将下一个节点的指针指向当前节点。

5. 将当前节点的指针指向null。

6. 返回保存的返回值。

以下是使用递归方法实现链表反转的Java代码:

class Node {
    int data;
    Node next;

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

class LinkedList {
    Node head;

    public LinkedList() {
        this.head = null;
    }

    // 方法:反转链表
    public Node reverse(Node node) {
        if (node == null || node.next == null) {
            return node;
        }

        Node nextNode = reverse(node.next);
        node.next.next = node;
        node.next = null;
        
        return nextNode;
    }

    // 方法:添加节点到链表
    public void addNode(int data) {
        Node newNode = new Node(data);
        
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    // 方法:打印链表
    public void printList() {
        Node current = head;
        
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();

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

        System.out.println("原链表:");
        list.printList();

        list.head = list.reverse(list.head);

        System.out.println("反转后的链表:");
        list.printList();
    }
}

在以上代码中,我们在LinkedList类中添加了一个新的reverse()方法。在main()方法中,我们创建了一个链表对象,添加了几个节点并打印原链表,然后调用reverse()方法反转链表并打印反转后的链表。

以上是Java函数实现链表的反转的方法和代码。无论是使用迭代还是递归,都能实现链表的反转,具体选择哪种方法取决于个人的偏好和实际情况。