本帖最后由 Stubborn 于 2020-3-13 03:32 编辑
循环单链表??最近在学数据结构,练习一下,效率很不好,不用测哈
class CircularSingleStrand:
class _Node:
def __init__(self, value):
self.val = value
self.next = None
def __init__(self):
self._head = None
def add(self, iterable):
self.l = len(iterable)
l = []
for val in iterable:
node = self._Node(val)
l.append(node)
node.next = self._head
self._head = node
l[0].next = l[-1]
del l
def result(self, n, m):
self.add(range(n-1, -1, -1))
c = 1
node = self._head
while self.l != 0:
pre_node = node
c += 1
node = node.next
if not c % m:
pre_node.next = node.next
self.l -= 1
return node.val
t = CircularSingleStrand()
print(t.result(12, 17))
|