python3 环形链表求解
孩子们的游戏(圆圈中最后剩下的数)
http://www.nowcoder.com/questionTerminal/f78a359491e64a50bce2d89cff857eb6
# -*- coding:utf-8 -*-
class Node():
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def LastRemaining_Solution(self, n, m):
# write code here
if not n:
return -1
kids = [i for i in range(1, n)]
root = Node(0)
copy_root = root
for i in kids:
root.next = Node(i)
root = root.next
root.next = copy_root # 组建环形链表成功
if m == 1:
return root.val
for i in range(n - 1):
for j in range(m - 1):
root = root.next
root.next = root.next.next
return root.val

