Skip to content

Added Problem2 and Problem4#1431

Open
megharaykar wants to merge 2 commits intosuper30admin:masterfrom
megharaykar:master
Open

Added Problem2 and Problem4#1431
megharaykar wants to merge 2 commits intosuper30admin:masterfrom
megharaykar:master

Conversation

@megharaykar
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You have correctly implemented the slow and fast pointer technique to find the middle of the list.
  • The reverse function is implemented correctly.
  • The merging step is handled correctly for both even and odd length lists.
  • For the intersection problem, you correctly calculated the lengths and adjusted the pointers.

Areas for Improvement:

  • In Problem2.py, the variable names in the merging step could be more descriptive. For example, instead of temp and temp1, you could use next_slow and next_fast to make the code clearer.
  • Although not necessary, you can simplify the merging step by using only one temporary variable for the next node of the first list. For example:
    while fast:
        next_slow = slow.next
        slow.next = fast
        fast = fast.next
        slow.next.next = next_slow
        slow = next_slow
    
    This is similar to the reference solution in C++.
  • In Problem2.py, note that the condition in the merging loop is correct, but it is important to understand that the first half might have one extra node which is already connected at the end. So no extra step is needed.
  • For Problem4.py, the solution is correct and efficient. However, you can avoid counting the lengths by using a two-pointer technique without calculating the length (by switching heads when one pointer reaches the end). This approach is also O(n) and O(1) but might be more elegant. But your solution is perfectly acceptable.

Overall, your solutions are correct and efficient. Keep up the good work!

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