Leetcode: Reverse Linked List
来源:互联网 发布:js压缩 tomcat 编辑:程序博客网 时间:2024/06/05 19:26
Question
Reverse a singly linked list.
click to show more hints.
Show Tags
Show Similar Problems
Solution 1
Analysis
recursive method
Code
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head==None or head.next==None: return head tail = head.next n = self.reverseList(tail) head.next = None tail.next = head return n
/** * 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) { if(head==NULL || head->next==NULL) return head; ListNode* second = head->next; ListNode* rest_head = reverseList(second); second->next = head; head->next = NULL; return rest_head; }};
Solution2
Analysis
iterative method
Example:
input: 1->2->3->4->Nonep l 1 2->3->4->Nonep l2->1 3->4->Nonep l3->2->1 4->Nonep l 4->3->2->1 None
Code
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head==None or head.next==None: return head p = head; # p is the head of reversed likedin list l = head.next # l is the head of the rest likedin list p.next = None while(l): t = l.next # after this, l is middle node between reversed list and the rest list l.next = p p = l l = t return p
/** * 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) { if( head==NULL || head->next==NULL) return head; ListNode* p = head; ListNode* l = head->next; head->next = NULL; while(l){ ListNode* t = l->next; l->next = p; p = l; l = t; } return p; }};
When thinking it in iterative way, we need to get what intermediate state looks like. For this problem, several nodes are reversed with header pointer, and the rest nodes with the header pointer.
Actually, recursive method reversed linked list one by one from the ending node to the beginning node. Iterative method does it from the starting node.
Iterative method can’t reverse node from the ending node. It is easy to know that additional time O(n) will be cost to obtain the previous node of p.
0 0
- [LeetCode]Reverse Linked List
- [Leetcode] Reverse Linked List
- leetcode Reverse Linked List
- [LeetCode] Reverse Linked List
- [LeetCode]Reverse Linked List
- Leetcode---Reverse Linked List
- [leetcode] Reverse Linked List
- [leetcode]Reverse Linked List
- Leetcode Reverse Linked List
- [leetcode] Reverse Linked List
- leetcode Reverse Linked List
- Reverse Linked List[LeetCode]
- leetcode--Reverse Linked List
- LeetCode Reverse Linked List
- leetcode: Reverse Linked List
- [leetcode] Reverse Linked List
- LeetCode - Reverse Linked List
- LeetCode || Reverse Linked List
- 1048 -- 求某一整数序列的全排列问题
- nusoap传递对象数组
- Web请求过程
- 字符串_模式匹配算法(求子串)
- 1049 -- 矩阵翻转
- Leetcode: Reverse Linked List
- 1050 -- C语言合法标识符
- 第一天
- 《剑指Offer》面试题:找到第一个只出现一次的字符
- 用例图、类图中有哪些关系
- Maven 项目打包发布
- 全国省市区数据库
- JNI make文件
- 微信公众号网页授权获取用户基本信息