题目:
思想:
三指针反转单链表,利用head少新建一个指针
代码:
/**
* 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 || head.next == null)
return head;
/**
* 三指针反转单链表(因为head被用到了,所以不需要多新建一个指针)
*/
ListNode nowNode = head.next,postNode;
head.next = null;
while(nowNode != null) {
postNode = nowNode.next;
nowNode.next = head;
head = nowNode;
nowNode = postNode;
}
return head;
}
}