您的位置:首页 > 编程语言 > Python开发

Python 实现列表 偶数位与奇数反转

2017-06-14 20:32 531 查看
1->2->3->4
转换成
2->1->4->3
class ListNode:def __init__(self, x):self.val = xself.next = Noneclass Sort:def swapList(self,head):if head is not None and head.next is not None:next = head.nexthead.next = self.swapList(next.next)next.next = headreturn nextreturn headl1 =ListNode(1)l2 =ListNode(2)l3 =ListNode(3)l4 =ListNode(4)l1.next = l2l2.next = l3l3.next = l4sor = Sort()l5 = sor.swapList(l1)while l5 is not None:print(l5.val)l5 =l5.next
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: