数据结构与算法
双指针、回溯剪枝、图论与搜索
🎨 视觉封面LeetCode Reverse Linked List
Problem
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?
即反转链表,建议采用递归和迭代两种方式。
Python3
# 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?
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# 迭代法
class SolutionIteratively:
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
pre_node = None
while head is not None:
pre_node, head.next, head = head, pre_node, head.next
return pre_node
# 递归法
class SolutionRecursively:
def reverseTail(self, cur, pre=None):
if not cur:
return pre
temp_node = cur.next
cur.next = pre
return self.reverseTail(temp_node, cur)
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
return self.reverseTail(head)
node_5 = ListNode(5)
node_4 = ListNode(4)
node_4.next = node_5
node_3 = ListNode(3)
node_3.next = node_4
node_2 = ListNode(2)
node_2.next = node_3
node_1 = ListNode(1)
node_1.next = node_2
solution = SolutionIteratively()
result = solution.reverseList(node_1)
print(result)
result_rec = SolutionRecursively().reverseList(node_5)
print(result_rec)
分析
迭代法中,只需要在遍历链表时重新构造链表即可。
递归法中,将开头的元素放置末位后,只需要将剩余的链表反序即可,如此构造一个递归函数。
所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI
欢迎加入:C++ GESP/CSP 考级答疑群(688906745) 与 Java/Python交流群(982860385),点击可直接加群。
猜你想读 · 相关文章推荐
LeetCode Minimum Depth of Binary Tree
Problem Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest lea...
LeetCode Path Sum
Problem Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For...
LeetCode Path Sum II
Problem Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum...
OneCoder (lihongzheshuai)
一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com