鱼C论坛

 找回密码
 立即注册
查看: 508|回复: 0

[技术交流] 【朱迪的LeetCode刷题笔记】】206. Reverse Linked List #Easy #Python #C++

[复制链接]
发表于 2023-5-30 09:23:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-5-29 21:03 编辑

Given the head of a singly linked list, reverse the list, and return the reversed list.

Example 1:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Example 2:
Input: head = [1,2]
Output: [2,1]

Example 3:
Input: head = []
Output: []


Constraints:
The number of nodes in the list is the range [0, 5000].
-5000 <= Node.val <= 5000

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


Judy
Python iterative version
  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution(object):
  7.     def reverseList(self, head):
  8.         """
  9.         :type head: ListNode
  10.         :rtype: ListNode
  11.         """
  12.         prev = None
  13.         curr = head
  14.         while curr:
  15.             temp = curr.next
  16.             curr.next = prev
  17.             prev = curr
  18.             curr = temp
  19.         return prev
复制代码


https://leetcode.com/problems/re ... 3/python-recursive/
Solution 1
Python recursive version
  1. class Solution:
  2.     def reverseList(self, head: Optional[ListNode], prev = None) -> Optional[ListNode]:
  3.         if head is None:
  4.             return prev
  5.         next = head.next
  6.         head.next = prev
  7.         return self.reverseList(next, head)
  8.         ```
复制代码


Mike
C++ iterative version

  1. ListNode* reverseList(ListNode* head) {
  2.         ListNode* current = head;
  3.         ListNode* prev = nullptr;
  4.         while (current != nullptr) {
  5.             ListNode* temp = current->next;
  6.             current->next = prev;
  7.             prev = current;
  8.             current = temp;
  9.         }
  10.         return prev;
  11.     }
复制代码

C++ recursive version

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. *     int val;
  5. *     ListNode *next;
  6. *     ListNode() : val(0), next(nullptr) {}
  7. *     ListNode(int x) : val(x), next(nullptr) {}
  8. *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13.     ListNode* reverseLast(ListNode* head, ListNode* prev) {
  14.         if (head->next == nullptr) {
  15.             head->next = prev;
  16.             return head;
  17.         }
  18.         ListNode* retval = reverseLast(head->next, head);
  19.         head->next = prev;
  20.         return retval;
  21.     }
  22.     ListNode* reverseList(ListNode* head) {
  23.         if (head == nullptr) {
  24.             return head;
  25.         }
  26.         return reverseLast(head, nullptr);
  27.     }
  28. };
复制代码


本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-23 16:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表