<jupyter_start><jupyter_text>Singly Linked List Cycle Check:**Problem Statement:** Given a singly linked list, write a function which takes in the first node in a singly linked list and returns a boolean indicating if the linked list contains a "cycle".A cycle is when a node's next point actually points back to a previous node in the list. This is also sometimes known as a circularly linked list.<jupyter_code>class Node(object):
    
    def __init__(self,value):
        
        self.value = value
        self.nextnode = None
def cycle_check(node):

    # create a node where both pointers start
    pointer1 = node
    pointer2 = node

    while pointer2 != None and pointer2.nextnode != None: # end of the list
        
        pointer1 = pointer1.nextnode
        pointer2 = pointer2.nextnode.nextnode # because pointer2 goes ahead of pointer1 by 1 node.

        if pointer2 == pointer1:
            return True

    # Case where pointer ahead reaches the end of the list
    return False
from nose.tools import assert_equal

# create a cycle list
a = Node(1)
b = Node(2)
c = Node(3)

a.nextnode = b
b.nextnode = c
c.nextnode = a # cycle


# create a non-cycle list
x = Node(1)
y = Node(2)
z = Node(3)

x.nextnode = y
y.nextnode = z


class TestCycleCheck(object):
    
    def test(self,sol):
        assert_equal(sol(a),True)
        assert_equal(sol(x),False)
        
        print("ALL TEST CASES PASSED")

t = TestCycleCheck()
t.test(cycle_check)<jupyter_output>ALL TEST CASES PASSED<jupyter_text>___________  Linked List ReversalWrite a function to reverse a Linked List in place. The function will take in the head of the list as input and return the new head of the list.<jupyter_code>class Node(object):
    def __init__(self, value):
        self.value = value
        self.nextnode = None
def reverse(head):
    
    current = head
    prevnode = None
    nextnode = None
    
    while current:
        
        nextnode = current.nextnode
        current.nextnode = prevnode
        # the current node should reverse its pointer to its previous node and to the next node too.
        
        prevnode = current
        current = nextnode
    
    return prevnode
a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
a.nextnode = b
b.nextnode = c
c.nextnode = d
print("a =", a.nextnode.value) # prints the value of b
print("b =", b.nextnode.value) # prints the value of c
print("c =", c.nextnode.value) # prints the value of d
print(d.nextnode.value) # none
reverse(a)
print(d.nextnode.value)
print(c.nextnode.value)
print(b.nextnode.value)
print(a.nextnode.value) # none<jupyter_output><empty_output><jupyter_text>______________  Linked List Nth to Last NodeWrite a function that takes a head node and an integer value n and then returns the nth to last node in the linked list.<jupyter_code>class Node(object):

    def __init__(self, value):
        self.value = value
        self.nextnode  = None
def n2lastnode(n, head):
    left_pointer = head
    right_pointer = head
    
    for i in range(n-1):
        if not right_pointer.nextnode:
            raise LookupError("Err! n is larger than the linked list.")
        right_pointer = right_pointer.nextnode # right pointer n nodes away from the head
        
        while right_pointer.nextnode:
            left_pointer = left_pointer.nextnode
            right_pointer = right_pointer.nextnode
        
        return left_pointer
# IMMEDIATE SOLUTION CHECK

from nose.tools import assert_equal

a = Node(1)
b = Node(2)
c = Node(3)
d = Node(4)
e = Node(5)

a.nextnode = b
b.nextnode = c
c.nextnode = d
d.nextnode = e

####

class TestNLast(object):
    
    def test(self,sol):
        
        assert_equal(sol(2,a),d)
        print('ALL TEST CASES PASSED')
        
# Run tests
t = TestNLast()
t.test(n2lastnode)<jupyter_output>ALL TEST CASES PASSED<jupyter_text>______________________________________  Implement a Linked ListImplement a Linked List by using a Node class object. Show how you would implement a Singly Linked List and a Doubly Linked List!**Singly Linked List**<jupyter_code>class Node1(object):
    def __init__(self, value):
        self.value = value
        self.nextnode = value
a = Node1(100)
b = Node1(200)
c = Node1(300)
a.nextnode = b
b.nextnode = c
print(a.nextnode.value)
print(b.nextnode.value)
print(c.value)<jupyter_output>200
300
300<jupyter_text>**Doubly Linked List**<jupyter_code>class Node2(object):
    def __init__(self, value):
        self.value = value
        self.nextnode = self.value
        self.prevnode = self.value
a = Node2(1000)
b = Node2(2000)
c = Node2(3000)
b.prevnode = a
a.nextnode = b
b.nextnode = c
c.prevnode = b
print(b.prevnode.value) # a<jupyter_output>1000
{'fraction_non_alphanumeric': 0.08787234042553191, 'fraction_numerical': 0.01574468085106383, 'mean_word_length': 3.0421324161650904, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '230513', 'license': 'MIT', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1494, 'n_words': 496, 'path': 'Linked Lists/Linked Lists Problems.ipynb', 'repo_name': 'dimensionsix/Data-Structures-Algorithms', 'size': '4700'}