Java Challenge - Reverse a Singly-Linked List

This community-built FAQ covers the “Reverse a Singly-Linked List” code challenge in Java. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Java challenge Reverse a Singly-Linked List

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I was a little lazy with this:
I made a function to get all the nodes in the linked list as an array;
then for reverseLinkedList ,
I iterated through that array,
setting .next for each node to be the previous node
and I returned the last node in the array.

I had to add in checking whether a node is null to prevent errors.

my code

I made functions to get the length of the linked list, and to make it into an array of nodes.

  public static int getSize(Node starting) {
    if (starting == null) {
      return 0;
    }
    int count = 1;
    Node current = starting;
    try {
      while (!(current.next == null)) {
        count++;
        current = current.next;
      }
    }
    finally {
      return count;
    }
  }

  public static Node[] nodeArray(Node head) {
    int length = getSize(head);
    Node[] list = new Node[length];
    if (length == 0) { return list; };
    list[0] = head;
    Node current = head;
    for (int i = 1; i < length; i++) {
      current = current.next;
      list[i] = current;
    }
    return list;
  }

  public static Node reverseLinkedList(Node linkedList) {
    Node[] nodes = nodeArray(linkedList);
    int length = nodes.length;
    if (length <= 1) {
      return linkedList;
    }
    Node previous = nodes[0]; // linkedList
    previous.next = null;
    Node current;
    for (int i = 1; i < length; i++) {
      current = nodes[i];
      current.next = previous;
      previous = current;
    }
    return nodes[length - 1];
  }

This was my best go, just started dipping into Java so may not be very effective.

public class Reverse{

  public static Node reverseLinkedList(Node linkedList){
    if(linkedList == null){
      return null;
    }

    Node currentNode = linkedList;
    int listSize = 1;
    while(currentNode.next != null){
      currentNode = currentNode.next;
      listSize++;
    }
    Integer[] newList = new Integer[listSize];

    currentNode = linkedList;

    for(int i = listSize-1; i >= 0; i--){
      newList[i] = currentNode.data;
      currentNode = currentNode.next;
    }

    Node newLinkedList = Node.makeLinkedList(newList);

    return newLinkedList;
  }

  public static void main(String[] args){
      Integer[] testArr = {4, 8, 15};
      Node testNode = Node.makeLinkedList(testArr);
      System.out.println("Original Linked List");
      testNode.print();
      Node reversed = Reverse.reverseLinkedList(testNode); 
      System.out.println("Reversed Linked List");
      reversed.print();
    }
}

I made a version that did not use an array of nodes:
but I had to keep track of 3 nodes: previous, current, and next

  public static Node reverseLinkedList(Node linkedList) {
    if (linkedList == null) {
      return linkedList;
    }
    if (linkedList.next == null) {
      return linkedList;
    }
    Node previous = null;
    Node current = linkedList;
    Node next = current.next;
    current.next = previous;
    while (next != null) {
      previous = current;
      current = next;
      next = current.next;
      current.next = previous;
    }
    return current;
  }

I guess I could have used a do-while loop here.