1、前言
题目描述2、思路
老题了。
3、代码
/**
* 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 q = head.next;
ListNode p = q;
head.next = null;
while(q != null){
p = q.next;
q.next = head;
head = q;
q = p;
}
return head;
}
}
网友评论