Unsolvable problem when m = 1


  • 0
    K

    I am thinking about will happen if the input m = 1 and n > 1 for any n.
    I am writing the solution in cpp, but the problem is since the head of the list passed into the function is passed by value, we cannot change the original head pointer. So if m =1, and for any n > 1, the nth element in the list should be the new head, but we cannot change this in our function.

    I think one possible solution for this is to add a sentinel node before the start of the list and pass the sentinel into the function, this will solve the problem I think.


  • 2
    S

    Note that the function returns a "ListNode*", which is the new head. You don't have to worry about not being able to 'change the original head'.


  • 0
    K

    That makes sense, thanks for answering!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.