Skip to content

Adding all the solutions#1781

Open
subbu4061 wants to merge 1 commit intosuper30admin:masterfrom
subbu4061:master
Open

Adding all the solutions#1781
subbu4061 wants to merge 1 commit intosuper30admin:masterfrom
subbu4061:master

Conversation

@subbu4061
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You provided both iterative and recursive solutions as suggested by the problem, showing a good understanding of different approaches.
  • The iterative solution is efficient with O(n) time and O(1) space, matching the optimal solution.
  • The code is generally readable and well-structured.

Areas for Improvement:

  • For the recursive solution, avoid using an instance variable (output) to store the new head. Instead, you can structure the recursion to return the new head. For example:
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
    
    This avoids the need for a helper function and an instance variable, making the code cleaner and purely recursive.
  • The submission includes solutions for other problems that are not related to reversing a linked list. When submitting a solution, ensure that you only include code relevant to the problem at hand to avoid confusion.
  • For the recursive solution, be aware that for very large lists (close to 5000 nodes), the recursion depth might cause a stack overflow. The iterative solution is preferred for large inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants