Leetcode-206. Reverse Linked List
2017-01-01 04:16
351 查看
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
Reverse a singly linked list.
这个题目挺简单的Your
runtime beats 31.82% of java submissions.
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode preNode = null, currentNode = head, nextNode = head.next;
while(currentNode != null && currentNode.next != null){
currentNode.next = preNode;
preNode = currentNode;
currentNode = nextNode;
nextNode = nextNode.next;
}
currentNode.next = preNode;
return currentNode;
}
}
博客链接:mcf171的博客
——————————————————————————————
Reverse a singly linked list.
这个题目挺简单的Your
runtime beats 31.82% of java submissions.
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode preNode = null, currentNode = head, nextNode = head.next;
while(currentNode != null && currentNode.next != null){
currentNode.next = preNode;
preNode = currentNode;
currentNode = nextNode;
nextNode = nextNode.next;
}
currentNode.next = preNode;
return currentNode;
}
}
相关文章推荐
- leetcode 206. Reverse Linked List
- LeetCode:206. Reverse Linked List
- leetcode 206. Reverse Linked List
- LeetCode-206. Reverse Linked List
- LeetCode 206. Reverse Linked List
- [LeetCode]206. Reverse Linked List
- LeetCode 206. Reverse Linked List
- Leetcode:206. Reverse Linked List
- leetCode_Add to List 206. Reverse Linked List
- LeetCode 206. Reverse Linked List
- Leetcode 206. Reverse Linked List cpp实现
- [LeetCode]206. Reverse Linked List
- [LeetCode] 206. Reverse Linked List
- [leetcode] 206. Reverse Linked List
- LeetCode 206. Reverse Linked List
- 【leetcode】206. Reverse Linked List
- leetcode 206. Reverse Linked List
- Leetcode 206. Reverse Linked List
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
- LeetCode 206. Reverse Linked List