Reverse a linked list

  • + 0 comments

    Java 15

     public static SinglyLinkedListNode reverse(SinglyLinkedListNode llist) {
            SinglyLinkedListNode prev = null;
            SinglyLinkedListNode current = llist;
            SinglyLinkedListNode nextNode = null;
    
            while (current != null) {
                nextNode = current.next;
                current.next = prev;
                prev = current;
                current = nextNode;
            }
            return prev;
        }