[关闭]
@Scrazy 2017-04-13T06:26:42.000000Z 字数 290 阅读 795

反转链表

python 算法


思路:
1. 创建一个新的头节点 pre
2. head 节点指向 pre
3. 循环

改变指针方向

1->2->3->4
1<-2<-3-<4
  1. # -*- coding:utf-8 -*-
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6. class Solution:
  7. # 返回ListNode
  8. def ReverseList(self, pHead):
  9. pre = None
  10. next = None
  11. while pHead:
  12. next = pHead.next
  13. pHead.next = pre
  14. pre = pHead
  15. pHead = next
  16. return pre
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注