一、题目
二、代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution
{
//目的 指针反着指
//前面有一个null
public ListNode reverseList(ListNode head)
{
//三个指针变化就行
if(head == null) return null;
ListNode temp = null;
ListNode pre = null;
ListNode cur = head;
while(cur!=null)
{
temp = cur.next;
cur.next = pre;
pre = cur;
cur =temp;
}
return pre;
}
}