试题:
输入一个链表,反转链表后,输出新链表的表头。
代码:
至少需要两个指针存储前后节点,然后发现还缺一个指针存储下下个节点。所以总共需要三个指针。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null) return null;
ListNode p=null,temp;
while(head != null){
temp = head.next;
head.next = p;
p = head;
head = temp;
}
return p;
}
}