Leetcode-206. Reverse Linked List

来源:互联网 发布:netstat 监听端口 编辑:程序博客网 时间:2024/06/05 20:47

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发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;    }}




0 0
原创粉丝点击