206. Reverse Linked List【easy】
206. Reverse Linked List【easy】
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
解法一:
- class Solution {
- public:
- ListNode* reverseList(ListNode* head) {
- ListNode * pre = NULL;
- while (head != NULL) {
- ListNode * next = head->next;
- head->next = pre;
- pre = head;
- head = next;
- }
- return pre;
- }
- };
解法二:
- class Solution {
- public:
- ListNode* reverseList(ListNode* head) {
- if (!head || !(head -> next)) return head;
- ListNode* node = reverseList(head -> next);
- head -> next -> next = head;
- head -> next = NULL;
- return node;
- }
- };
参考了@jianchao.li.fighter 的代码
The basic idea of this recursive solution is to reverse all the following nodes after head
. Then we need to set head
to be the final node in the reversed list. We simply set its next node in the original list (head -> next
) to point to it and sets its next
to be NULL
.
解法三:
- public ListNode reverseList(ListNode head) {
- /* recursive solution */
- 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);
- }
参考了@braydenCN 的代码
206. Reverse Linked List【easy】的更多相关文章
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 234. Palindrome Linked List【Easy】【判断链表是否回文】
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 876. Middle of the Linked List【Easy】【单链表中点】
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
随机推荐
- IDEA的Maven项目找不到class
- 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones
打表找规律即可. 1,1,2,2,2,3,3,3,3,4,4,4,4,4... 注意打表的时候,sg值不只与剩下的石子数有关,也和之前取走的方案有关. //#include<cstdio> ...
- 【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...
- 【树链剖分】【线段树】bzoj3083 遥远的国度
记最开始的根为root,换根之后,对于当前的根rtnow和询问子树U而言, ①rtnow==U,询问整棵树 ②fa[rtnow]==U,询问除了rtnow所在子树以外的整棵树 ③rtnow在U的子树里 ...
- IOS学习笔记39--NSString各种语法的使用
今天就NSString的各种语法学习学习,以后慢慢补充: 1.字符串的遍历 NSString *string = @"CHENGWULI"; //字符串的长度 int count ...
- 阿里云乌班图16配置-PHP环境(包括mysql及apache安装)
1. 安装apache apt-get update apt-get install apache2 service apache2 restart 通过浏览器访问服务器的IP,如果出现Apac ...
- Android 智能指针学习 一
Android5.1 中智能指针涉及的文件如下: system/core/include/utils/RefBase.h system/core/libutils/RefBase.cpp system ...
- TELNET终端类型选项
转:http://www.cnpaf.net/Class/Telnet/200408/5.html 1. 命令名称及编号TERMINAL-TYPE242.命令含义IACWILLTERMINAL-TYP ...
- win8.1无法安装安装.net framework 3.5 解决办法【转】
安装流程1.以系统管理员开启命令提示符(命令提示字符)2挂载windows8.1异3,在命令提示符下输入Dism /online /enablefeature/featurename:NetFx3 / ...
- UVA-10603-Fill(BFS+优先队列)
There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not grea ...