コード例 #1
0
def main():
    linked_list = LinkedList(0)
    for i in range(10):
        linked_list.insert(i+1)
    linked_list.print_linked_list()
    linked_list.delete(3)
    linked_list.print_linked_list()
コード例 #2
0
def main():
    first_list = LinkedList()
    second_list = LinkedList()
    for i in range(3):
        first_list.insert(i)
    for i in range(3):
        second_list.insert(i)
コード例 #3
0
    def test_insert_node_in_the_begining(self):

        ll = LinkedList(5)
        ll.append_node(6)
        ll.append_node(10)
        ll.append_node(20)
        
        ll.insert_node_in_the_begining(7)
        
        result_ll = ll.get_linkedlist()
        actual_list = [7,5,6,10,20]
        self.assertEqual(result_ll,actual_list)
コード例 #4
0
    def test_delete_node(self):

        ll = LinkedList(5)
        ll.append_node(6)
        ll.append_node(10)
        ll.append_node(20)

        ll.delete_node(6)

        result_ll = ll.get_linkedlist()
        actual_list = [5,10,20]
        self.assertEqual(result_ll,actual_list)
コード例 #5
0
def rearrange(list):
    first = list
    slow = first
    fast = slow
    secound = None
    new_list = LinkedList()
    new_node = None
    flag = True

    while fast.get_next() and fast.get_next().get_next():
        slow = slow.get_next()
        fast = fast.get_next().get_next()

    secound = slow.get_next()
    slow.set_next(None)

    l1 = LinkedList(secound)
    l1.reverse_list()
    secound = l1.head

    while first and secound:
        if new_node is None:
            new_node = Node(first.get_data())
            new_list.head = new_node
            first = first.get_next()
            flag = False

        elif flag is True:
            new_node.set_next(Node(first.get_data()))
            new_node = new_node.get_next()
            first = first.get_next()
            flag = False

        elif flag is False:
            new_node.set_next(Node(secound.get_data()))
            new_node = new_node.get_next()
            secound = secound.get_next()
            flag = True

    if secound is not None:
        new_node.set_next(secound)
    if first is not None:
        new_node.set_next(first)

    return new_list.head
コード例 #6
0
class Queue:
    def __init__(self):
        self.size = 0
        self.storage = LinkedList()

    def __len__(self):
        return self.size

    def enqueue(self, value):
        # self.storage.append(value)
        self.storage.add_to_tail(value)
        self.size += 1

    def dequeue(self):
        if self.size == 0:
            return None
        self.size -= 1
        return self.storage.remove_head()
コード例 #7
0
ファイル: queue.py プロジェクト: nezlobnaya/Data-Structures
class Queue:
    def __init__(self):
        self.size = 0
        self.storage = LinkedList()

    def __len__(self):
        return self.size

    def enqueue(self, value):
        self.size += 1
        return self.storage.add_to_tail(value)

    def dequeue(self):
        if self.size > 0:
            self.size -= 1
            return self.storage.remove_head()

    def len(self):
        return len(self.storage)
コード例 #8
0
 def test_validdata(self):
     
     ll = LinkedList(5)
     ll.append_node(6)
     ll.append_node(10)
     ll.append_node(20)
     result_ll = ll.get_linkedlist()
     actual_list = [5,6,10,20]
     self.assertEqual(result_ll,actual_list)
コード例 #9
0
class Stack:
    def __init__(self):
        self.size = 0
        # self.storage = []
        self.storage = LinkedList()

    def __len__(self):
        return self.size

    def push(self, value):
        # self.storage.append(value) #adding values to the list
        self.size += 1 #incrementing size so when we ask for lenth we get the correct answer
        self.storage.add_to_tail(value)


    def pop(self):
        if self.size == 0:
            return None

        self.size -= 1
        return self.storage.remove_tail()
コード例 #10
0
from single_linked_list import LinkedList, Node


def getCount(head_node):
    #code here
    cnt = 0
    temp = head_node
    while temp != None:
        cnt += 1
        temp = temp.next
    return cnt


if __name__ == '__main__':
    # t = int(input())
    # for cases in range(t):
    n = int(input())
    a = LinkedList()
    nodes = list(map(int, input().strip().split()))
    for data in nodes:
        node = Node(data)
        a.append(node)
    print(getCount(a.head))
コード例 #11
0
ファイル: linked_lists.py プロジェクト: Alviner/algo
    def test_merge(self):
        s_list_one = LinkedList()
        s_list_one.add_in_tail(Node(1))
        s_list_one.add_in_tail(Node(2))
        s_list_one.add_in_tail(Node(3))
        s_list_one.add_in_tail(Node(4))
        s_list_one.add_in_tail(Node(5))
        s_list_one.add_in_tail(Node(6))
        s_list_one.add_in_tail(Node(7))
        s_list_one.add_in_tail(Node(8))
        s_list_one.add_in_tail(Node(9))
        s_list_one.add_in_tail(Node(10))
        s_list_one.add_in_tail(Node(11))
        s_list_one.add_in_tail(Node(12))
        s_list_one.add_in_tail(Node(13))

        s_list_two = LinkedList()
        s_list_two.add_in_tail(Node(13))
        s_list_two.add_in_tail(Node(12))
        s_list_two.add_in_tail(Node(11))
        s_list_two.add_in_tail(Node(10))
        s_list_two.add_in_tail(Node(9))
        s_list_two.add_in_tail(Node(8))
        s_list_two.add_in_tail(Node(7))
        s_list_two.add_in_tail(Node(6))
        s_list_two.add_in_tail(Node(5))
        s_list_two.add_in_tail(Node(4))
        s_list_two.add_in_tail(Node(3))
        s_list_two.add_in_tail(Node(2))
        s_list_two.add_in_tail(Node(1))

        s_list_merged = merge_list(s_list_two, s_list_one)

        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))

        self.assertEqual(s_list_merged.len(), self.list_test.len())

        node = s_list_merged.head
        node_test = self.list_test.head
        while node is not None and node_test is not None:
            self.assertEqual(node.value, node_test.value)
            node = node.next
            node_test = node_test.next
コード例 #12
0
from single_linked_list import Node, LinkedList

def kth_to_last(self, k):
    fast = self.head
    slow = self.head
    for i in xrange(k):
        fast = fast.next

    while fast.next:
        fast = fast.next
        slow = slow.next

    return slow

setattr(LinkedList, 'kth_to_last', kth_to_last)

val_list = [1, 2, 3, 4, 5]
linked_list = LinkedList.from_val_list(val_list)
print linked_list
k = 2
print 'the {k}th to last is {node}'.format(k=k, node=linked_list.kth_to_last(k))
コード例 #13
0
    """

    if linked_list.is_empty():
        return False

    fast = linked_list.head.next
    slow = linked_list.head.next

    # 快慢指针查找中位数
    while fast is not None and fast.next is not None:
        fast = fast.next.next
        slow = slow.next




if __name__ == '__main__':
    # 创建一个链表对象
    linked_list = LinkedList()

    print('链表是否为空:%d' % linked_list.is_empty())

    linked_list.append('l')
    linked_list.append('e')
    linked_list.append('v')
    linked_list.append('e')
    linked_list.append('l')

    print('遍历单链表:')
    linked_list.traverse()
コード例 #14
0
from single_linked_list import LinkedList, Node

def getNth(head, k):
    # Code here
    idx = 0
    temp = head
    while idx != k-1:
        temp = temp.next
        idx += 1
    return temp.data

if __name__ == "__main__":
    T = int(input())
    while T > 0:
        llist = LinkedList()
        n, k = list(map(int, input().split()))
        nodes = list(map(int, input().split()))
        for data in reversed(nodes):
            llist.push(data)
        m = getNth(llist.head, k)
        print(m)
        T -= 1
コード例 #15
0
ファイル: PrintListFromEnd.py プロジェクト: devNaresh/DS-Algo
__author__ = '__naresh__'

## WAP to print list from the END

"""
------------ Algo ------------

Simple Just use Recursion. Once End reach start printing data

"""

from single_linked_list import LinkedList


def print_list_from_end(head):
    if head is None:
        return
    print_list_from_end(head.get_next())
    print head.get_data()


if __name__ == "__main__":
    l1 = LinkedList()
    l1.create_list([40, 35, 30, 25, 20, 15, 10, 5, 1, 2, 75, 85])

    print_list_from_end(l1.head)
コード例 #16
0
ファイル: linked_lists.py プロジェクト: Alviner/algo
class TestLinkedList(unittest.TestCase):
    def setUp(self):
        self.list = LinkedList()
        self.list_test = LinkedList()

    def test_remove(self):
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(10))
        self.list.add_in_tail(Node(3))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(4))
        self.list.add_in_tail(Node(1))

        self.list_test.add_in_tail(Node(2))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(2))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(1))

        self.list.delete(1)
        self.list.delete(10)
        self.list.delete(3)
        self.list.delete(4)
        self.assertEqual(self.list.len(), self.list_test.len())

        node = self.list.head
        node_test = self.list_test.head
        while node is not None and node_test is not None:
            self.assertEqual(node.value, node.value)
            node = node.next
            node_test = node_test.next

    def test_mass_remove(self):
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))

        self.list_test.add_in_tail(Node(2))
        self.list_test.add_in_tail(Node(2))
        self.list_test.add_in_tail(Node(128))
        self.list_test.add_in_tail(Node(128))

        self.list.delete(1, all=True)
        self.list.delete(9, all=True)
        self.assertEqual(self.list.len(), self.list_test.len())

        node = self.list.head
        node_test = self.list_test.head
        while node is not None and node_test is not None:
            self.assertEqual(node.value, node_test.value)
            node = node.next
            node_test = node_test.next

    def test_clear(self):
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))

        self.list.clean()

        self.assertEqual(self.list.len(), 0)

        self.assertIsNone(self.list.head, 'Проверяем, что голова пустая')
        self.assertIsNone(self.list.tail, 'Проверяем, что хвост пуст')

    def test_search(self):
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))

        self.assertEqual(self.list.find(9), self.list.head)
        self.assertEqual(self.list.find(1), self.list.head.next.next)
        self.assertIsNone(self.list.find(120))

    def test_mass_search(self):
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))

        self.assertEqual(len(self.list.find_all(9)), 4)
        self.assertEqual(len(self.list.find_all(1)), 5)
        self.assertEqual(len(self.list.find_all(128)), 2)

    def test_len(self):
        self.list.add_in_tail(Node(1))
        self.assertEqual(self.list.len(), 1)
        self.list.add_in_tail(Node(1))
        self.assertEqual(self.list.len(), 2)
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(2))
        self.assertEqual(self.list.len(), 5)
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.assertEqual(self.list.len(), 10)
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.assertEqual(self.list.len(), 12)
        self.list.add_in_tail(Node(9))
        self.assertEqual(self.list.len(), 13)

    def test_insert(self):
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(2))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(128))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))
        self.list.add_in_tail(Node(1))
        self.list.add_in_tail(Node(9))

        self.list_test.add_in_tail(Node(-9))
        self.list_test.add_in_tail(Node(-10))
        self.list_test.add_in_tail(Node(-11))
        self.list_test.add_in_tail(Node(-2))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(2))
        self.list_test.add_in_tail(Node(-1))
        self.list_test.add_in_tail(Node(-3))
        self.list_test.add_in_tail(Node(2))
        self.list_test.add_in_tail(Node(128))
        self.list_test.add_in_tail(Node(128))
        self.list_test.add_in_tail(Node(9))
        self.list_test.add_in_tail(Node(9))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(9))
        self.list_test.add_in_tail(Node(1))
        self.list_test.add_in_tail(Node(9))
        self.list_test.add_in_tail(Node(-111))

        self.list.insert(None, Node(-11))
        self.list.insert(None, Node(-10))
        self.list.insert(None, Node(-9))
        self.list.insert(self.list.head.next.next.next.next.next.next,
                         Node(-1))
        self.list.insert(self.list.head.next.next, Node(-2))
        self.list.insert(
            self.list.head.next.next.next.next.next.next.next.next, Node(-3))
        self.list.insert(self.list.tail, Node(-111))

        self.assertEqual(self.list.len(), self.list_test.len())

        node = self.list.head
        node_test = self.list_test.head
        while node is not None and node_test is not None:
            self.assertEqual(node.value, node_test.value)
            node = node.next
            node_test = node_test.next

        self.list.clean()
        self.list_test.clean()

        node = Node(2)

        self.list.insert(None, node)

        self.assertEqual(self.list.head, node)
        self.assertEqual(self.list.tail, node)

        self.list_test.add_in_tail(node)
        self.assertEqual(self.list_test.head, node)
        self.assertEqual(self.list_test.tail, node)

    def test_merge(self):
        s_list_one = LinkedList()
        s_list_one.add_in_tail(Node(1))
        s_list_one.add_in_tail(Node(2))
        s_list_one.add_in_tail(Node(3))
        s_list_one.add_in_tail(Node(4))
        s_list_one.add_in_tail(Node(5))
        s_list_one.add_in_tail(Node(6))
        s_list_one.add_in_tail(Node(7))
        s_list_one.add_in_tail(Node(8))
        s_list_one.add_in_tail(Node(9))
        s_list_one.add_in_tail(Node(10))
        s_list_one.add_in_tail(Node(11))
        s_list_one.add_in_tail(Node(12))
        s_list_one.add_in_tail(Node(13))

        s_list_two = LinkedList()
        s_list_two.add_in_tail(Node(13))
        s_list_two.add_in_tail(Node(12))
        s_list_two.add_in_tail(Node(11))
        s_list_two.add_in_tail(Node(10))
        s_list_two.add_in_tail(Node(9))
        s_list_two.add_in_tail(Node(8))
        s_list_two.add_in_tail(Node(7))
        s_list_two.add_in_tail(Node(6))
        s_list_two.add_in_tail(Node(5))
        s_list_two.add_in_tail(Node(4))
        s_list_two.add_in_tail(Node(3))
        s_list_two.add_in_tail(Node(2))
        s_list_two.add_in_tail(Node(1))

        s_list_merged = merge_list(s_list_two, s_list_one)

        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))
        self.list_test.add_in_tail(Node(14))

        self.assertEqual(s_list_merged.len(), self.list_test.len())

        node = s_list_merged.head
        node_test = self.list_test.head
        while node is not None and node_test is not None:
            self.assertEqual(node.value, node_test.value)
            node = node.next
            node_test = node_test.next
コード例 #17
0
from single_linked_list import Node, LinkedList


def is_palindrome(self):
    fast = self.head
    slow = self.head
    stack = []

    while fast and fast.next:
        fast = fast.next.next
        slow = slow.next
        stack.append(slow.val)

    if not fast:
        stack.pop()

    slow = slow.next
    while slow and stack.pop() == slow.val:
        slow = slow.next
    return (True if not slow else False)


setattr(LinkedList, 'is_palindrome', is_palindrome)

val_list_1 = [1, 2, 3, 4, 5]
linked_list_1 = LinkedList.from_val_list(val_list_1)
print linked_list_1.is_palindrome()
val_list_2 = [1, 2, 3, 2, 1]
linked_list_2 = LinkedList.from_val_list(val_list_2)
print linked_list_2.is_palindrome()
コード例 #18
0
ファイル: linked_lists.py プロジェクト: Alviner/algo
 def setUp(self):
     self.list = LinkedList()
     self.list_test = LinkedList()
コード例 #19
0
    n1 = l1.head.next
    head = Node()
    current = head

    carry = 0
    while n0 or n1 or carry:
        addition = carry
        if n0:
            addition += n0.val
            n0 = n0.next
        if n1:
            addition += n1.val
            n1 = n1.next
        current.next = Node(val=(addition % 10))
        current = current.next
        carry = addition / 10

    return cls(head.next, True)


setattr(LinkedList, "from_addition", from_addition)

val_list_1 = [3, 7, 5]
linked_list_1 = LinkedList.from_val_list(val_list_1)
val_list_2 = [9, 7, 3, 7, 2]
linked_list_2 = LinkedList.from_val_list(val_list_2)
linked_list_3 = LinkedList.from_addition(linked_list_1, linked_list_2)
print linked_list_1
print linked_list_2
print linked_list_3
コード例 #20
0
def from_addition(cls, l0, l1):
    n0 = l0.head.next
    n1 = l1.head.next
    head = Node()
    current = head

    carry = 0
    while n0 or n1 or carry:
        addition = carry
        if n0:
            addition += n0.val
            n0 = n0.next
        if n1:
            addition += n1.val
            n1 = n1.next
        current.next = Node(val=(addition % 10))
        current = current.next
        carry = addition / 10

    return cls(head.next, True)

setattr(LinkedList, 'from_addition', from_addition)

val_list_1 = [3, 7, 5]
linked_list_1 = LinkedList.from_val_list(val_list_1)
val_list_2 = [9, 7, 3, 7, 2]
linked_list_2 = LinkedList.from_val_list(val_list_2)
linked_list_3 = LinkedList.from_addition(linked_list_1, linked_list_2)
print linked_list_1
print linked_list_2
print linked_list_3
コード例 #21
0
    main_node = Node(0)
    node = main_node

    while l1 is not None and l2 is not None:
        if l2.get_data() <= l1.get_data():
            node.set_next(Node(l1.get_data()))
            node = node.get_next()
            l1 = l1.get_next()
        elif l1.get_data() <= l2.get_data():
            node.set_next(Node(l2.get_data()))
            node = node.get_next()
            l2 = l2.get_next()
    if l1 is None:
        node.set_next(l2)
    else:
        node.set_next(l1)
    return main_node.get_next()

if __name__ == '__main__':
    l1 = LinkedList()
    l2 = LinkedList()

    l1.create_list([40, 30, 15, 5])
    l2.create_list([45, 40, 10])

    out = merge(l1.head, l2.head)

    while out is not None:
        print(out.get_data(), end=' ')
        out = out.get_next()
コード例 #22
0
        elif flag is True:
            new_node.set_next(Node(first.get_data()))
            new_node = new_node.get_next()
            first = first.get_next()
            flag = False

        elif flag is False:
            new_node.set_next(Node(secound.get_data()))
            new_node = new_node.get_next()
            secound = secound.get_next()
            flag = True

    if secound is not None:
        new_node.set_next(secound)
    if first is not None:
        new_node.set_next(first)

    return new_list.head

if __name__ == '__main__':
    l1 = LinkedList()

    l1.create_list([40, 35, 30, 25, 20, 15, 10])

    out = rearrange(l1.head)

    while out is not None:
        print(out.get_data(), end=' ')
        out = out.get_next()
コード例 #23
0
from single_linked_list import Node, LinkedList

def is_palindrome(self):
    fast = self.head
    slow = self.head
    stack = []

    while fast and fast.next:
        fast = fast.next.next
        slow = slow.next
        stack.append(slow.val)

    if not fast:
        stack.pop()

    slow = slow.next
    while slow and stack.pop() == slow.val:
        slow = slow.next
    return (True if not slow else False)

setattr(LinkedList, 'is_palindrome', is_palindrome)

val_list_1 = [1, 2, 3, 4, 5]
linked_list_1 = LinkedList.from_val_list(val_list_1)
print linked_list_1.is_palindrome()
val_list_2 = [1, 2, 3, 2, 1]
linked_list_2 = LinkedList.from_val_list(val_list_2)
print linked_list_2.is_palindrome()
コード例 #24
0
from single_linked_list import Node, LinkedList

def loop_head(self):
    fast = self.head
    slow = self.head
    while fast and fast.next:
        fast = fast.next.next
        slow = slow.next
        if fast == slow:
            break
    if not fast or not fast.next:
        return None
    
    p0 = self.head
    p1 = fast
    while p0 != p1:
        p0 = p0.next
        p1 = p1.next
    return p0

setattr(LinkedList, 'loop_head', loop_head)

val_list = [1, 2, 3, 4, 5]
linked_list = LinkedList.from_val_list(val_list)
linked_list.last().next = linked_list.head.next.next.next
print linked_list.loop_head().val
コード例 #25
0
ファイル: queue.py プロジェクト: nezlobnaya/Data-Structures
 def __init__(self):
     self.size = 0
     self.storage = LinkedList()
コード例 #26
0
ファイル: ReverseBlockOfK.py プロジェクト: devNaresh/DS-Algo
        current = next_node

    return prev


def reverse_block(head, k):
    start = end = pointer = head
    temp = 0

    while end is not None:
        if temp == k:
            if start == head:
                head = reverse_list(start, end)
                pointer = start
            else:
                pointer.set_next(reverse_list(start, end))
                pointer = start
            start = end
            temp = 0
        temp += 1
        end = end.get_next()
    return head


if __name__ == "__main__":
    l1 = LinkedList()
    l1.create_list([40, 35, 30, 25, 20, 15, 10, 5, 1, 2, 75, 85])

    l1.head = reverse_block(l1.head, 2)
    l1.print_data(l1.head)