您的位置:首页 > 编程语言 > Java开发

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java leetcode 算法