Reverse a single linked list

Define a function that takes the head node of a linked list, reverses the list, and outputs the head node of the reversed list

public class Number24 {
    
   public Node reverseLinkedList(Node node) {
        Node pre = null;
        Node next = null;

        while(node ! =null){
            next = node.next;
            node.next = pre;
            pre = node;
            node = next;
        }
        return pre;
    }

  public class Node {
        public int value;
        public Node next;

        void Node(int value) {
            this.value = value; }}}Copy the code