Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
How would you change the code if you have the remaining sublist values shouldn't...

Donald

Feb 24, 2022

How would you change the code if you have the remaining sublist values shouldn't be changed? ie. instead of 8 -> 7 you leave it at 7 -> 8

3

0

Comments
Comments
P
Perry Robinson4 years ago

Please show how to modify solution so that if a group does not fit within k, it does not get modified. Similar to leetcode 25.

S
SeungJin Kim4 years ago

Would also like to know

S
SeungJin Kim4 years ago

temp = current should_end = False for i in range(k): if not temp: should_end = True break temp = temp.next if should_end == True: break

I added this piece of check just before we do the reverse sublist and the answer is accepted for leetcode 25

K
Kenny Le3 years ago

might not be the most efficient way, but you can first check that a reversal can be completed or not.

java solution, add this for loop before the reversing for loop

ListNode temp = curr; for(int i = 0; i < k; i++){ if(temp == null){ return head; } else{ temp = temp.ne...