What is wrong in my code… Split A circular linked list

Question:

Question
https://practice.geeksforgeeks.org/problems/split-a-circular-linked-list-into-two-halves/1

Given a Cirular Linked List of size N, split it into two halves circular lists. If there are odd number of nodes in the given circular linked list then out of the resulting two halved lists, first list should have one node more than the second list. The resultant lists should also be circular lists and not linear lists.

Example 1:

Input:

Circular LinkedList:

1->5->7

Output:

1 5

7

Example 2:

Input:

Circular LinkedList:

2->6->1->5

Output:

2 6

1 5

My Code:

def splitList(head, head1, head2):
    slow = head
    fast = head
    head1 = head
    while fast.next!=head and fast.next.next!=head:
        slow = slow.next
        fast = fast.next.next
    if fast.next.next == head:
        fast = fast.next
    head2 = slow.next
    fast.next = slow.next
    slow.next = head
Asked By: Vishav Singla

||

Answers:

Checkout this logic:

def splitList(head, head1, head2):
    slow = head
    fast = head

    if head is None:
        return

    while(fast.next != head and fast.next.next != head):
        fast = fast.next.next
        slow = slow.next

    if fast.next.next == head:
        fast = fast.next

    head1.head = head

    if head.next != head:
        head2.head = slow.next

    fast.next = slow.next
    slow.next = head
Answered By: Martian Coder

I don’t think there is anything wrong.
What exactly is not working for you? how do you initialize your circular linked list?

def splitList(head, head1, head2):
    slow = head
    fast = head
    head1 = head
    while fast.next!=head and fast.next.next!=head:
        slow = slow.next
        fast = fast.next.next
    if fast.next.next == head:
        fast = fast.next
    head2 = slow.next
    fast.next = slow.next
    slow.next = head
    return head1, head2

class Node():
    def __init__(self,value):
        self.value = value
        self.next = None
        
    def show(self):
        print(self.value, end="->")

# create a circular linked list
def get_cllist(a_list):
    head = Node(a_list[0])
    curr = head
    for el in a_list[1:]:
        curr.next = Node(el)
        curr = curr.next
    curr.next = head
    return head

def print_llist(llist):
    head = llist
    print("linked list: ", end="")
    while llist.next != None and llist.next != head:
        llist.show()
        llist = llist.next
    llist.show()
    print()
    
        
llist = get_cllist([1,2,3,4,5,6,7])
print_llist(llist)
head1, head2 = splitList(llist, None, None)
print_llist(head1)
print_llist(head2)
    
linked list: 1->2->3->4->5->6->7->
linked list: 1->2->3->4->
linked list: 5->6->7->
Answered By: Luka

Your code is actually using the correct slow/fast logic, and the problems you encounter relate to some specifics of the code challenge set up:

  • Your function does not return anything. I must say that the code challenge is badly designed at this point. The author of the challenge must have had a programming language in mind that supports call-by-reference, as they provide boiler plate code that "works around" the absence of this feature in Python. But that leads to the weird signature of this function, where it gets two arguments which are utterly useless (head1 and head2). The idea is that the Python function must return two node references, and that part is missing in your solution. This is what it says in the boiler plate code that the challenge site provides you with:

    class Solution:
         def splitList(self, head, head1, head2):
             #code here
    
    
             #this is to emulate pass by reference in python please don't delete below line.
             return head1,head2
    

    And so, what you were told not to do, … you did: you removed that return head1,head2

  • Your function signature lacks the self parameter. Possibly you removed it temporarily while you were testing in a local environment, where you removed the class Solution wrapper, but be sure to add that again when submitting your solution on the code challenge site.

  • It is given that the input list will have at least one node, but it could be just one node, and in that case your code produces head1 and head2 references that both reference that single node. This is wrong. When the input has just one node, head1 should reference that node, but head2 should be None. It is probably easiest if you detect that boundary case with an if. For instance, just before the return statement do this:

    if head1 == head2:
        head2 = None
    

If you correct these issues it will work fine. For completeness sake, here is the code adjusted with the above mentioned points:

class Solution:
    # Part of boilerplate code: needs `self` (and two useless arguments)
    def splitList(self, head, head1, head2):
        slow = head
        fast = head
        head1 = head
        while fast.next!=head and fast.next.next!=head:
            slow = slow.next
            fast = fast.next.next
        if fast.next.next == head:
            fast = fast.next
        head2 = slow.next
        fast.next = slow.next
        slow.next = head
        # Add this to deal with boundary case
        if head1 == head2:
            head2 = None
        # Below this point is boilerplate code that needed to stay
        #this is to emulate pass by reference in python please don't delete below line.
        return head1,head2
Answered By: trincot
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.