首页 > 其他 > 详细

12-206. Reverse Linked List

时间:2019-06-01 00:22:05      阅读:122      评论:0      收藏:0      [点我收藏+]

题目描述:

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

代码实现:

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 
 7 class Solution:
 8     def reverseList(self, head: ListNode) -> ListNode:
 9         prev = None
10         while head:
11             curr = head
12             head = head.next
13             curr.next = prev
14             prev = curr
15         return prev

 

12-206. Reverse Linked List

原文:https://www.cnblogs.com/tbgatgb/p/10958053.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!