206--Reverse A Singly Linked List
package LinedList;
public class ReverseASinglyLinkedList {
//解法一:迭代。
public ListNode reverseList(ListNode head) {
ListNode previous = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next;
current.next = previous;
previous = current;
current = next;
}
return current;
}
/**
* 解法二:递归
* 递归的思路其实和迭代一样,就是处理两个节点,让它们之间的指针反转。
* 只是,迭代是从头到尾依次处理,需要一个额外的指针来指向下一个节点。
* 而递归是从尾到头,回溯的时候会有当前的节点,所以不需要额外的指针。
* 还需要注意的是:p是反转后的整个链表的头节点,它第一次找到后,就没有作任何处理。
* 而不是每次要处理(反转)的那个节点。
*/
public ListNode reverseList2(ListNode head) {
if (head==null||head.next==null)
return head;
ListNode p=reverseList2(head.next);
head.next.next=head;
head.next=null;
return p;
}
}
206--Reverse A Singly Linked List的更多相关文章
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- Reverse a singly linked list
Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...
- [轉]Reverse a singly linked list
Reverse a singly linked list http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- Java [Leetcode 206]Reverse Linked List
题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...
- 【LeetCode】206. Reverse Linked List
题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...
- 【一天一道LeetCode】#206. Reverse Linked List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- LeetCode 206. Reverse Linked List倒置链表 C++
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
随机推荐
- Slack完整教学与上手心得:找到正确的团队沟通之道
Slack完整教学与上手心得:找到正确的团队沟通之道 时间 2015-06-13 09:21:42 逐鹿网 原文 http://www.zhulu.com/article/5519.html 主题 ...
- 史上最全NOIP初赛知识点
CSP-J/S 第一轮知识点选讲 \(NOIP\)(全国青少年信息学奥林匹克竞赛)于2019年取消.取而代之的是由\(CCF\)推出的非专业级软件能力认证,也就是现在的\(CSP-J/S\).作为一名 ...
- 团队冲刺--three
今天学习css,用css做登录界面. 昨天学习了爬虫了初步. 问题:爬虫很难.
- The 13th Chinese Northeast Collegiate Programming Contest
题解: solution Code: A. Apple Business #include<cstdio> #include<algorithm> #include<ve ...
- 手机爬虫--appium
adb 安装:下载android-sdk压缩包,解压后其中有adb.exe,配置环境变量后即可 cmd下'adb'即可启动adb客户端 adb devices –l 查看已连接的模拟器 adb co ...
- [LeetCode] 287. Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- JavaWeb 笔记
WEB-INF 目录 web.xml 文件配置 精准匹配 "/" "/index" "/go/index.html" 路径通配匹配 &quo ...
- Mac:输出phpinfo的信息
输出phpinfo的信息1.echo '<?php phpinfo(); ?>' | php 2>&12.php -a 进入Interactive shell模式然后:pri ...
- 基于Tendermint的区块链漂流瓶简单实现
本文主要借demo介绍基于Tendermint的区块链应用开发,这个demo很简单,主要包含以下功能: 扔漂流瓶 捞漂流瓶 之后投放者和打捞者可以相互传递[加密]信息 代码已上传至github. Te ...
- 第22课 weak_ptr弱引用智能指针
一. weak_ptr的概况 (一)weak_ptr的创建 1. 直接初始化:weak_ptr<T> wp(sp); //其中sp为shared_ptr类型 2. 赋值: wp1 = sp ...