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;
}
}