Reverse a Linked List in Groups of Given Size

This problem was a POTD of GFG for the month of July, I think this was yesterday's potd, which I had finished just minutes before the midnight on the clock. The practicals are finished today although there wasn't much that I gave for them out of my daily time but still they take a lot of daytime. I have finished almost everything for today, if I don't count the lecture so what about a blog? 

I thought that this problem was quite interesting in terms of the simplicity it shows but the complexity it possesses, I feel that this was fairly a long time, since I handled or did something related to Linked Lists, and the problem also took much more time than I would expect, because of that reason I guess.

This problem basically examined or visualisation, or maybe your memory if you had done something like this before. 

Overall, I felt that the banal idea of having to store the first number of every group and then when we get to the end number of the next group, say y, then making that number(or node whatever) point to y would do the needful, making sure that we are continuously making the elems point to the elem previous to it, because that's what we wanna do, reverse the elems of a group.

After all this mess, the last problem was making sure that we handle the condition where the group may not be complete, means there can be a group of size 5 which has just 2 elems.

Last but not the least and the most time consuming part was to make the overall last elem of the result linked list point to None, so that there would be some ending to the linked list and the list won't just point to 2 elems, back and forth making the linked list and infinite one.

So, I feel that this would be really interesting for someone who atleast have some logical thinking base for a linked list traversal or the basic operations of an ll, like removing some elems at some positions or like that.

The code has comments that are hopefully informative enough, the interesting part to look forward to is that there are 2 prevGroupElems, that is, 1 and 2, bcz if you would think about it, then you would notice that we'll have to store the first elem of the next group also before we can reach the end of that group,

for ex.: 1,2,3,     4,5,6 ,    7,8,9

with k=3, means here we have these 3 groups.

so will store 1 in prevGroupElem1 and then before I can get to 6, will also have to store 4 for later use, so that I don't have to come back for it, so that would be temporarily in the prevGroupElem2, and after getting to 6 will make 1 point to 6 and will put 4 in prevGroupElem1, making the prevGroupElem2 empty for the storage of next group's 1st element(that is 7).

I think that's it for this one.

Cya ;) 

Code:-

"""Return reference of new head of the reverse linked list
  The input list will have at least one element
  Node is defined as

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
  This is method only submission.
  You only need to complete the method.
"""
class Solution:
    def reverse(self,head, k):
        temp=head
        prevGroupElem1=None
        prevGroupElem2=None
        # newHead=None
        first=True
       
        while(temp!=None):
            # print(temp.data)
            # temp=temp.next
           
            # Do the below steps before starting of every group  
            if(prevGroupElem1==None):
                # just for the 1st time update the prevGroupElem1, otherwise
                # everytime
                # update the prevGroupElem2 further
                # print("First group and so setting prevGroupElem1 to this, that is
                # ",temp.data)
                prevGroupElem1=temp
                # prevGroupElem1.next=None
                # if(temp==None):
                    # print("HERE WE HAD A NONE VALUE!")
                # prevGroupElem1.next=None
            else:
                # print("Setting prevGroupElem2 to",temp.data)
                # prevGroupElem1.next=temp
                # prevGroupElem1=temp
                prevGroupElem2=temp
                # prevGroupElem1.next=None
           
            count=1
           
            prevNode=None
            # go for every group, one by one
            while(count<=k):
                if(temp==None):
                    # print("Temp became None in between the group counting")
                    # print("Count was",count)
                    return head
                # Saving the next node of the current node for later usage
                nextTempNode=temp.next
                temp.next=None
                # print("Here next node is",nextTempNode.data if(nextTempNode!=None)
                # else "(Next node is None!)")
               
                if(prevNode==None):
                    # for the 1st elem of every group, just update the prevNode,
                    # that's it
                    # print("Setting prevNode for first time in this group, to",
                    # temp.data)
                    prevNode=temp
                else:
                    # Means we gotta connect this temp node to that node
                    # and also update the temp node to the next node after this one
                   
                    temp.next=prevNode
                    # print("Setting the prevNode to",temp.data)
                    prevNode=temp
                   
                   
                if(count==k):
                    # If we are at the last element
                    if(first!=True):
                        # will have to make the prevGroupElem1 point to this node
                        prevGroupElem1.next=temp
                       
                        # and will also update the prevGroupElem1 to now the
                        # prevGroupElem2, so that the next new elem in a
                        # new group can be kept in the prevGroupElem2
                        prevGroupElem1=prevGroupElem2
                        # print("This wasn't first group")
                        pass
                    if(first==True):
                        # newHead=temp
                        # print("This was 1st group...setting the head to this
                        # node, that is",temp.data)
                        head=temp
                        first=False
                   
                    temp=nextTempNode
                    # print("This was group ",count)
                    # Means this group has been done and finished
                    break
               
                if(nextTempNode==None):
                    # If we won't have a next elem, then just update
                    prevGroupElem1.next=temp
                temp=nextTempNode
               
                count+=1
        # prevGroupElem1.next=None
        return head

Comments

Popular Posts