<jupyter_start><jupyter_text>Return node where cycle begins in Linked list.Given a linked list, return the node where the cycle begins. If thDENote: Do not modify the linked list.Follow up:Can you solve it without using extra space?https://leetcode.com/problems/linked-list-cycle-ii/description/<jupyter_code>class listnode():
    val  = None
    next = None
    
    def __init__(self, val):
        self.val = val
    
    def __str__(self, maxc=20):       
        if self.next and maxc > 1:
            return '[' + self.val + '] ' + self.next.__str__(maxc-1)
        else:
            return '[' + self.val + '] DONE'

    def cycleto(self, linkval):
        node = self
        linkto = None
        lastnode = None
        while node:
            if node.val == linkval and linkto == None:
                linkto = node
            lastnode = node
            node = node.next            
        lastnode.next = linkto
        
    def cycle_size(self):
        ''' Given a node in a cycle get its side (as a count of nodes)
        '''
        n = 0
        node = self
        while node != None:
            node = node.next
            n += 1
            if node == self:
                return n
        return 0

    def has_cycle(self):
        head = self
        tail = self.next
        while tail != None:
            if head == tail: return True
            head = head.next
            tail = tail.next
            if head == tail: return True
            if tail: tail = tail.next
        return False


        return -1

    def cycle_node(self):
        head = self
        tail = self.next
        n = 0
        while tail != None:
            if head == tail: return head
            head = head.next
            tail = tail.next
            if head == tail: return head
            if tail: tail = tail.next
        return None

    def cycle_nodes(self):
        ''' Return the nodes that starts and end a cycle '''
        node = None
        nodeinloop = self.cycle_node()
        if nodeinloop:
            n = nodeinloop.cycle_size()
            head = tail = self
            # Advance tail by exactly the loop size.
            for i in range(n): 
                prev = tail
                tail = tail.next           
            while head != tail:
                head = head.next
                tail = tail.next
                prev = prev.next                
            return (head, prev)
        return (None, None)

    def cycle_start(self):
        ''' Return the node that starts a cycle '''
        return self.cycle_nodes()[0]
        
    def cycle_end(self):
        ''' Return the node that ends a cycle '''
        return self.cycle_nodes()[1]
    
    @staticmethod
    def builder(nodes, linkval = None):
        head = None
        prev = None 
        for ch in list(nodes):
            node = listnode(ch)
            if head:
                prev.next = node
                prev = node
            else:
                head = node
                prev = head
        if linkval:
            head.cycleto(linkval)
        return head

a = listnode.builder('ABCDEFGHIJKLM', 'E')
print(a)
print(a.cycle_node().val)
print(a.cycle_node().cycle_size())
print(a.cycle_start().val)
print(a.cycle_end().val)
16 // 9 *
list('lkdsjfgljsfd')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09088173337345772, 'fraction_numerical': 0.004213060487511285, 'mean_word_length': 1.3982683982683983, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '653599', 'license': 'Unlicense', 'n_tokens_mistral': 947, 'n_tokens_neox': 911, 'n_words': 296, 'path': 'LinkedListCycles.ipynb', 'repo_name': 'jfogarty/python-code-practice', 'size': '3323'}