Java实现单链表的反转
阅读原文时间:2023年07月08日阅读:2

思路1初始化一个新的头节点reverseHead,然后遍历旧链表,利用头插法向reverseHead进行插入

思路2: 1.反转相当于数据的更换(1和n,2和n-1,3和n-2)n为链表的长度 2.通过遍历进行数据的更换,n/2为循环退出的条件

package com.company;

import java.util.Stack;

/**
 * @author:抱着鱼睡觉的喵喵
 * @date:2021/2/4
 * @description:
 */
public class LinkedListDemo {
    public static void main(String[] args) {
        Node node1 = new Node(1, 96, "Ronin");
        Node node2 = new Node(2, 100, "lisi");
        Node node3 = new Node(3, 99, "张三");
        Node node4 = new Node(4, 63, "zsh");
        Node node5 = new Node(5, 65, "zms");

        SingleLinkedList singleLinkedList = new SingleLinkedList();

        singleLinkedList.add(node1);
        singleLinkedList.add(node2);
        singleLinkedList.add(node2);
        singleLinkedList.add(node4);
        singleLinkedList.add(node5);

        System.out.println("链表反转后的数据如下:");
        getReverse2(singleLinkedList.getNode());
        singleLinkedList.list();
    }

    /**
     * 链表的反转 方法1
     * 思路:1.反转相当于数据的更换(1和n,2和n-1,3和n-2)n为链表的长度
     * 2.通过遍历进行数据的更换,n/2为循环退出的条件
     *
     * @param head
     * @return
     */
    public static void getReverse(Node head) {
        if (head.next == null) {
            System.out.println("LinkedList is empty!");
            return;
        }
        int length = getLength(head);
        int num1 = 0;
        int num2 = 0;
        Node mid = new Node();
        for (int i = 1, j = length; i <= length / 2; i++, j--) {
            Node temp = head;
            Node cur = head;
            while (true) {
                temp = temp.next;
                num1++;
                if (num1 == i) {
                    num1 = 0;
                    break;
                }
            }
            while (true) {
                cur = cur.next;
                num2++;
                if (j == num2) {
                    num2 = 0;
                    break;
                }
            }
            mid.sno = temp.sno;
            mid.score = temp.score;
            mid.data = temp.data;
            temp.sno = cur.sno;
            temp.score = cur.score;
            temp.data = cur.data;
            cur.sno = mid.sno;
            cur.score = mid.score;
            cur.data = mid.data;

        }
        Node temp2 = head.next;
        while (temp2 != null) {
            System.out.println(temp2);
            temp2 = temp2.next;
        }
    }

    /**
     * 链表的反转2
     * 思路:
     * 初始化一个新的头节点reverseHead,然后遍历链表head,利用头插法向reverseHead进行插入
     *
     * @param head
     */
    public static void getReverse2(Node head) {
        if (head.next == null) {
            System.out.println("LinkedList is empty!");
            return;
        }
        Node reverseHead = new Node(0, 0, "");
        Node cur = null;
        Node temp = head.next;
        while (temp != null) {
            cur = temp.next;
            temp.next = reverseHead.next;
            reverseHead.next = temp;
            temp = cur;
        }
        head.next = reverseHead.next;
    }

//节点类
class Node {
    public Node next;
    public int sno;
    public int score;
    public String data;

    public Node() {
    }

    public Node(int Sno, int NScore, String Data) {
        this.sno = Sno;
        this.score = NScore;
        this.data = Data;
    }

    @Override
    public String toString() {
        return "Node{" +
                "sno=" + sno +
                ", score=" + score +
                ", data='" + data + '\'' +
                '}';
    }
}

//链表操作类
class SingleLinkedList {
    private Node head = new Node(0, 0, ""); //初始化头节点

    public Node getNode() {
        return head;
    }

    // add student data
    public void add(Node node) {        //数据添加
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
    }

    //output
    public void list() {            //遍历数据进行打印
        Node temp = head.next;
        if (temp == null) {
            System.out.println("LinkedList is empty!");
        } else {
            while (temp != null) {
                System.out.println(temp);
                System.out.println();
                temp = temp.next;
            }
        }
    }
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章