《剑指Offer》刷题之反转链表
我不知道将去向何方,但我已在路上! |
---|
时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! |
- 题目:
输入一个链表,反转链表后,输出新链表的表头。 - 示例:
示例 1 :
输入:{1,2,3}
返回值:{3,2,1}
- 代码1:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def ReverseList(self, pHead):
point = None
while pHead is not None:
temp = pHead.next
pHead.next = point
point = pHead
pHead = temp
return point
- 算法说明:
建立point
作为空指针进行过渡;
建立temp
存储下一个链表节点。