private static Node recurReverseLinkedList(Node firstNode) {if (firstNode == null || firstNode.next == null) {return firstNode;}Node secondNode = firstNode.next; // the new head node to be reversedfirstNode.next = null; // de-link the node from the listNode previousFirstNode = recurReverseLinkedList(secondNode); //reverse restsecondNode.next = firstNode; //the second node next will be first nodereturn previousFirstNode;}
Labels
Recursively reverse a linked list
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment