[LeetCode] Reverse Linked List(递归与非递归反转链表)
来源:互联网 发布:简历封面 知乎 编辑:程序博客网 时间:2024/06/14 03:57
Reverse a singly linked list.
解题思路
对于非递归实现,思路是依次将从第二个结点到最后一个结点的后继设为头结点,然后将该节点设为头结点(需记住将原头结点的后继设为空)。
对于递归实现,首先反转从第二个结点到最后一个结点的链表,然后再将头结点放到已反转链表的最后,函数返回新链表的头结点。
非递归实现代码1[C++]
//Runtime:10 msclass Solution {public: ListNode* reverseList(ListNode* head) { if (head == NULL) return NULL; ListNode *pre = head; ListNode *cur = head->next; while (cur != NULL) { pre->next = cur->next; cur->next = head; head = cur; cur = pre->next; } return head; }};
非递归实现代码2[C++]
//Runtime:22 msclass Solution{public: ListNode* reverseList(ListNode* head){ if (head == NULL) return NULL; ListNode *cur = head->next; head->next = NULL; while (cur != NULL) { ListNode *temp = cur->next; cur->next = head; head = cur; cur = temp; } return head; }};
非递归实现代码3[Java]
//Runtime:276 mspublic class Solution { public ListNode reverseList(ListNode head) { if (head == null) { return head; } ListNode cur = head.next; ListNode pre = head; while (cur != null){ pre.next = cur.next; cur.next = head; head = cur; cur = pre.next; } return head; }}
非递归实现代码4[Python]
#Runtime: 63 ms# Definition for singly-linked list.class ListNode: def __init__(self, x): self.val = x self.next = Noneclass Solution: # @param {ListNode} head # @return {ListNode} def reverseList(self, head): if head == None: return head pre = head cur = head.next while cur != None: pre.next = cur.next cur.next = head head = cur cur = pre.next return heads = Solution()head = ListNode(0);cur = headfor i in range(1, 10): node = ListNode(i) cur.next = node cur = nodehead = s.reverseList(head);while(head != None): print(head.val, end=' ') head = head.next
递归实现代码[C++]
//Runtime:10 msclass Solution{public: ListNode* reverseList(ListNode* head){ //此处的条件不能写成if(head == NULL) if (head == NULL || head->next == NULL) return head; ListNode *newhead = reverseList(head->next); head->next->next = head; head->next = NULL; return newhead; }};
C++测试代码如下:
void printList(ListNode *head){ while(head != NULL) { cout<<head->val<<" "; head = head->next; } cout<<endl;}void dropList(ListNode *head){ if (head == NULL) return; ListNode *temp; while (head != NULL) { temp = head->next; delete head; head = temp; }}int main(){ ListNode *head = new ListNode(0); ListNode *cur = head; for (int i = 0; i < 10; i++) { ListNode *newnode = new ListNode(floor(rand()%100)); cur->next = newnode; cur = newnode; } printList(head); Solution s; head = s.reverseList(head); printList(head); dropList(head);}
Java测试代码如下:
public class AA { public static void main(String[] args) { // TODO Auto-generated method stub ListNode head = new ListNode(0); ListNode cur = head; for(int i = 1; i < 10; i++){ ListNode node = new ListNode(i); cur.next = node; cur = node; } Solution s = new Solution(); head = s.reverseList(head); print(head); } static void print(ListNode head){ while(head != null){ System.out.println(head.val); head = head.next; } }}
2 0
- [LeetCode] Reverse Linked List(递归与非递归反转链表)
- LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
- 反转链表(递归与非递归)
- 链表反转--递归与非递归
- leetcode刷题系列--206. Reverse Linked List 递归和非递归 c++实现
- LeetCode-Reverse Linked List II(反转链表)
- Leetcode Reverse Linked List II 反转部分单向链表
- [LeetCode-206] Reverse Linked List(链表反转)
- [leetcode] Reverse Linked List II 链表反转
- [LeetCode]206. Reverse Linked List 链表反转
- LeetCode.206. Reverse Linked List(反转有序链表)
- LeetCode 206. Reverse Linked List 反转链表
- scala反转链表 leetcode 206 Reverse Linked List
- leetcode 92. Reverse Linked List II 反转链表
- LeetCode--Reverse Linked List 反转链表(Python)
- 链表反转(递归与非递归实现)
- C++递归与非递归实现链表的反转
- 反转链表[递归,非递归]
- DSP优化案例1
- 中国大陆开源镜像站汇总
- 从源码的角度解析View的事件分发
- Leetcode Add and Search Word - Data structure design
- 三天独立开发的iOS端CSDN博客阅读器上线了
- [LeetCode] Reverse Linked List(递归与非递归反转链表)
- [leetcode][string] Length of Last Word
- C语言中的main返回值
- ubuntu12.04+hadoop2.2.0+zookeeper3.4.5+hbase0.96.2+hive0.13.1伪分布式环境部署
- Andriod 从源码的角度详解View,ViewGroup的Touch事件的分发机制
- JSRender之渲染模板
- 模仿ATM取款机功能
- POJ - 2152 Fire 树型DP
- R语言(5)——因子