206. 反转链表
反转一个单链表。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null)
return head;
ListNode next = head.next;
head.next = null;
while(next != null){
ListNode next2 = next.next;
next.next = head;
head = next;
next = next2;
}
return head;
}
}