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

【小熊刷题】Reverse Linked List <Leetcode 206, Java>

2015-09-10 07:19 519 查看

Question

Reverse a singly linked list.

*Difficulty: easy

https://leetcode.com/problems/reverse-linked-list/

没啥好说的,思路清楚就行,今儿可以洗洗睡了。

/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode curr = head;
ListNode prev = null;

while(curr != null){
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: