Reverse a singly linked list.
可以将原来的list的节点依次尾插另一个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) {
ListNode p = new ListNode(0);
ListNode q = null;
if(head == null){
return head;
}
while(head != null){
q = head;
head = head.next;
q.next = p.next;
p.next = q;
}
return p.next;
}
}