Splitting starting N nodes into new Circular Linked List while preserving the old nodes | Python3 implementation of the approach ; Function to add a node to the empty list ; If not empty ; Assigning the data ; Creating the link ; Function to add a node to the beginning of the list ; If list is empty ; Assign data ; Function to traverse and prthe list ; If list is empty ; Pointing to the first Node of the list ; Traversing the list ; Function to find the length of the CircularLinkedList ; Stores the length ; List is empty ; Iterator Node to traverse the List ; Return the length of the list ; Function to split the first k nodes into a new CircularLinkedList and the remaining nodes stay in the original CircularLinkedList ; Empty Node for reference ; Check if the list is empty If yes , then return NULL ; NewLast will contain the last node of the new split list itr to iterate the node till the required node ; Update NewLast to the required node and link the last to the start of rest of the list ; Return the last node of the required list ; Driver code ; Append the new last node into the new list ; Print the new lists
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def addToEmpty ( last , data ) : NEW_LINE INDENT if ( last != None ) : NEW_LINE INDENT return last NEW_LINE DEDENT temp = Node ( data ) NEW_LINE last = temp NEW_LINE last . next = last NEW_LINE return last NEW_LINE DEDENT def addBegin ( last , data ) : NEW_LINE INDENT if ( last == None ) : NEW_LINE INDENT return addToEmpty ( data ) NEW_LINE DEDENT temp = Node ( data ) NEW_LINE temp . next = last . next NEW_LINE last . next = temp NEW_LINE return last NEW_LINE DEDENT def traverse ( last ) : NEW_LINE INDENT if ( last == None ) : NEW_LINE INDENT print ( " List ▁ is ▁ empty . " ) NEW_LINE return NEW_LINE DEDENT p = last . next NEW_LINE while True : NEW_LINE INDENT print ( p . data , end = " ▁ " ) NEW_LINE p = p . next NEW_LINE if p == last . next : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT def length ( last ) : NEW_LINE INDENT x = 0 NEW_LINE if ( last == None ) : NEW_LINE INDENT return x NEW_LINE DEDENT itr = last . next NEW_LINE while ( itr . next != last . next ) : NEW_LINE INDENT x += 1 NEW_LINE itr = itr . next NEW_LINE DEDENT return ( x + 1 ) NEW_LINE DEDENT def split ( last , k ) : NEW_LINE INDENT passs = Node ( - 1 ) NEW_LINE if ( last == None ) : NEW_LINE INDENT return last NEW_LINE DEDENT itr = last NEW_LINE for i in range ( k ) : NEW_LINE INDENT itr = itr . next NEW_LINE DEDENT newLast = itr NEW_LINE passs . next = itr . next NEW_LINE newLast . next = last . next NEW_LINE last . next = passs . next NEW_LINE return newLast NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT clist = None NEW_LINE clist = addToEmpty ( clist , 12 ) NEW_LINE clist = addBegin ( clist , 10 ) NEW_LINE clist = addBegin ( clist , 8 ) NEW_LINE clist = addBegin ( clist , 6 ) NEW_LINE clist = addBegin ( clist , 4 ) NEW_LINE clist = addBegin ( clist , 2 ) NEW_LINE print ( " Original ▁ list : " , end = " " ) NEW_LINE traverse ( clist ) NEW_LINE k = 4 NEW_LINE clist2 = split ( clist , k ) NEW_LINE print ( " The ▁ new ▁ lists ▁ are : " , end = " " ) NEW_LINE traverse ( clist2 ) NEW_LINE traverse ( clist ) NEW_LINE DEDENT