acm模式刷题经验交流帖
题目1

class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def solution(head):
if head.next==None:
return head
p = head
q = None
while p!=None:
temp = p.next
p.next = q
q = p
p = temp
return q
if __name__ == '__main__':
rec = input()
if len(rec)<=2:
print('{}')
else:
rec2 = list(map(int, rec[1:len(rec)-1].split(',')))
#读取输入
#print(rec2)
#生成初始链表
head = ListNode()
head.val = rec2[0]
temp = head
if len(rec2)>=2:
for i in range(1,len(rec2)):
temp.next = ListNode()
temp.next.val = rec2[i]
temp = temp.next
result = solution(head)
res = []
while result:
res.append(result.val)
result = result.next
#print(res)
res_str = (','.join(str(i) for i in res))
res_str = '{'+res_str+'}'
print(res_str)
acm模式处理输入输出


本文分享了ACM模式下解决链表问题的经验,包括`classListNode`类的实现和`solution`函数的递归策略。通过实例演示如何处理输入输出,重点在于理解链表操作和递归调用在比赛环境中的应用技巧。

6133

被折叠的 条评论
为什么被折叠?



