LeetCode 206. Reverse Linked List
原题链接在这里:https://leetcode.com/problems/reverse-linked-list/
题目:
Reverse a singly linked list.
题解:
Iteration 方法:
生成tail = head, cur = tail, while loop 的条件是tail.next != null. 最后返回cur 就好。
Time Complexity: O(n).
Space O(1).
AC Java:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 public class Solution { 10 public ListNode reverseList(ListNode head) { 11 if(head == null || head.next == null){ 12 return head; 13 } 14 ListNode tail = head; 15 ListNode cur = head; 16 ListNode pre; 17 ListNode temp; 18 while(tail.next != null){ 19 pre = cur; 20 cur = tail.next; 21 temp = cur.next; 22 cur.next = pre; 23 tail.next = temp; 24 } 25 return cur; 26 } 27 }
AC JavaScript:
1 /** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4 * this.val = val; 5 * this.next = null; 6 * } 7 */ 8 /** 9 * @param {ListNode} head 10 * @return {ListNode} 11 */ 12 var reverseList = function(head) { 13 if(!head || !head.next){ 14 return head; 15 } 16 17 var tail = head; 18 var cur = tail; 19 var pre = null; 20 var temp = null; 21 while(tail.next){ 22 pre = cur; 23 cur = tail.next; 24 temp = cur.next; 25 cur.next = pre; 26 tail.next = temp; 27 } 28 29 return cur; 30 };
AC C++:
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* reverseList(ListNode* head) { 14 if(!head || !head->next){ 15 return head; 16 } 17 18 ListNode *tail = head; 19 ListNode *cur = head; 20 ListNode *pre; 21 ListNode *temp; 22 while(tail->next){ 23 pre = cur; 24 cur = tail->next; 25 temp = cur->next; 26 cur->next = pre; 27 tail->next = temp; 28 } 29 30 return cur; 31 } 32 };
AC Python:
1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, val=0, next=None): 4 # self.val = val 5 # self.next = next 6 class Solution: 7 def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 if not head or not head.next: 9 return head 10 11 tail = cur = head 12 pre = temp = None 13 while(tail.next): 14 pre = cur 15 cur = tail.next 16 temp = cur.next 17 cur.next = pre 18 tail.next = temp 19 20 return cur
Recursion 方法:
reverseList(head.next)返回的是从head.next开始的reverse list,把head加在他的尾部即可。
他的尾部恰巧是之前的head.next, 这里用nxt表示。
Recursion 终止条件是head.next == null, 而不是head == null, head==null只是一种corner case而已。
Time Complexity: O(n), 先下去再回来一共走两遍.
Space O(n), 迭代用了stack一共O(n)大小, n 是原来list的长度。
AC Java:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 public class Solution { 10 public ListNode reverseList(ListNode head) { 11 //Method: Recursion 12 if(head == null || head.next == null){ 13 return head; 14 } 15 16 ListNode nxt = head.next; 17 ListNode newHead = reverseList(nxt); 18 19 nxt.next = head; 20 head.next = null; 21 return newHead; 22 } 23 }
跟上Reverse Linked List II, Reverse Nodes in k-Group, Binary Tree Upside Down, Palindrome Linked List, Maximum Twin Sum of a Linked List.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步