[LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
反向链表,分别用递归和迭代方式实现。
递归Iteration:
新建一个node(value=任意值, next = None), 用一个变量 next 记录head.next,head.next指向新node.next,新 node.next 指向head,next 进入下一个循环,重复操作,直到结束。
迭代Recursion:
假设有链表1 -> 2 -> 3 -> 4 -> 5要进行反转,第一层先把1 -> 2 -> 3 -> 4 -> 5和None传入递归函数(head, newhead),先记录head的next(2 -> 3 -> 4 -> 5),head(1)指向newhead(None)变成(1->None),然后把(2 -> 3 -> 4 -> 5, 1 -> None)递归传入下一层,记录head的next(3 -> 4 -> 5),head(2)指向newhead(1->None)变成(2 -> 1->None),然后把(3 -> 4 -> 5, 2 -> 1 -> None)递归传入下一层,直到最后head=None时返回newhead,此时 newhead = 5 -> 4 -> 3 -> 2 -> 1 -> None
Java: Iterative solution
1 2 3 4 5 6 7 8 9 10 | public ListNode reverseList(ListNode head) { ListNode newHead = null ; while (head != null ) { ListNode next = head.next; head.next = newHead; newHead = head; head = next; } return newHead; } |
Java: Recursive solution
1 2 3 4 5 6 7 8 9 10 11 | public ListNode reverseList(ListNode head) { return reverseListInt(head, null ); } private ListNode reverseListInt(ListNode head, ListNode newHead) { if (head == null ) return newHead; ListNode next = head.next; head.next = newHead; return reverseListInt(next, head); } |
Python: Iteration
1 2 3 4 5 6 7 8 | def reverseList( self , head): prev = None while head: curr = head head = head. next curr. next = prev prev = curr return prev |
Python: Iteration
1 2 3 4 5 6 7 8 9 | def reverseList( self , head): """ :type head: ListNode :rtype: ListNode """ cur, prev = head, None while cur: cur. next , prev, cur = prev, cur, cur. next return prev |
Python: Iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # Definition for singly-linked list. class ListNode: def __init__( self , x): self .val = x self . next = None def __repr__( self ): if self : return "{} -> {}" . format ( self .val, repr ( self . next )) class Solution: def reverseList( self , head): dummy = ListNode( 0 ) while head: next = head. next head. next = dummy. next dummy. next = head head = next return dummy. next |
Python: Recrusion
1 2 3 4 5 6 7 | class Solution( object ): def reverseList( self , head, prev = None ): if not head: return prev curr, head. next = head. next , prev return self .reverseList(curr, head) |
Python: Recursion
1 2 3 4 5 6 7 8 9 | class Solution: def reverseList( self , head): return self .doReverse(head, None ) def doReverse( self , head, newHead): if head is None : return newHead next = head. next head. next = newHead return self .doReverse( next , head) |
Python: Recursion, wo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution( object ): def reverseList( self , head): dummy = ListNode( 0 ) cur = self .recur(head, dummy) return cur. next def recur( self , head1, head2): if not head1: return head2 next_node = head1. next head1. next = head2. next head2. next = head1 return self .recur(next_node, head2) |
C++: Iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public : ListNode* reverseList(ListNode* head) { auto dummy = ListNode{0}; while (head) { auto tmp = head->next; head->next = dummy.next; dummy.next = head; head = tmp; } return dummy.next; } }; |
C++: Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Solution { public : /** * @param head: The first node of linked list. * @return: The new head of reversed linked list. */ ListNode *reverse(ListNode *head) { if (!head || !head->next) return head; ListNode *t = head; head = reverse(t->next); t->next->next = t; t->next = NULL; return head; } }; |
类似题目:
All LeetCode Questions List 题目汇总
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构