leetcode day7
这道题弄的心好累。。
【Reverse Linked List】206
描述:
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
思路:
增加一个全局节点newHead,用来指向新的头部,用用一个临时节点next来遍历整个链表,用来当旧的头部head,例如链表1-2-3-4,第一遍循环,newhead指向1,指向null,head是2,指向剩余的链表,2-3-4,然后第二遍循环,newhead指向2-1,head指向剩余的链表,3-4。。。以此类推,直到循环结束。
// iterative solution
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while(head != null){
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
还有递归的算法:
// recursive solution
public ListNode reverseList(ListNode head) {
return reverseListInt(head, null);
} public ListNode reverseListInt(ListNode head, ListNode newHead) {
if(head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}
leetcode day7的更多相关文章
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- leetcode每日刷题计划-简单篇day7
还没有背单词,头晕脑胀 Num 66 加一 Plus One 注意就是进位的时候最后一位,为了省两句代码,那几个语句顺序写反覆盖的乱七八糟 vector头部插入(a.begin(),被插入的数) 如果 ...
- 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和
[算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- fzu Problem - 2232 炉石传说(二分匹配)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2232 Description GG学长虽然并不打炉石传说,但是由于题面需要他便学会了打炉石传说.但是传统的炉石 ...
- odd or even?
public boolean isEven(int data){ if((data&1)== 0) return true; return false; } much faster than ...
- sphinx query multiple indexes in php
http://stackoverflow.com/questions/17494784/searching-a-particular-index-using-sphinx-from-multiple- ...
- fiddler2 中文乱码问题
打开注册表编辑器,找到HKCU\Software\Microsoft\Fiddler2\,在里面添加一个字符串值,名叫HeaderEncoding,值设置为默认编码.建议设成GB18030.然后要记得 ...
- java在CMD环境下执行需注意字符集设定
最近有个小工具需要将DMS系统中随机文件名替换为原始文件名,当导出原始文件名到csv文件中,用小 工具读取然后rename时,发现在eclipse环境下运行正常,简繁中文名称也正常:但放到cmd中执行 ...
- Block 再学习 !
如何优雅的使用 Block? How Do I Declare A Block in Objective-C? 阮一峰的一句话解释简洁明了:闭包就是能够读取其它函数内部变量的函数 详情:http:// ...
- expected: file:///
[java] java.lang.IllegalArgumentException: Wrong FS: hdfs://192.168.190.128:9000/user/hadoop/output/ ...
- eclipse工程名出现小红叉的解决办法
前提是eclipse工程中每个子文件都没错,工程名上却显示了小红叉. 打开[Window]->[Show View]->[General]->[Problems],看看Problem ...
- PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)
树的遍历. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- JS——基础知识(二)
1.变量提升问题 <script> var num=10; fun(); function fun(){ console.log(num); var num=20; } </scri ...