date:
updated:

nowcoder练习


NC78 反转列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if not pHead:
return None
newpHead = None
while pHead:
pHead.next, newpHead, pHead = newpHead, pHead, pHead.next
return newpHead

令后一个结点指向前一个结点,并且将表头指针指向最后一个结点即可。


← Prev 知识点随手记 | wsl_ubuntu的某些操作 Next →