Algo. Traverse linked list

// while

Node curr = head;
while (curr != null) {
  
  curr = curr.next;
}

// recursion

    public static Node traverseWirhRecursion(Node curr) {
        if (curr == null) {
            return null;
        }
        
        System.out.println("diving: " + curr.val);

        Node nodeBeforeFromRight = traverseWirhRecursion(curr.next);

        if (curr != null) {
            System.out.println("returning: " + curr.val);
        }

        return curr;
    }
This entry was posted in Без рубрики. Bookmark the permalink.