输入一个链表,反转链表后,输出新链表的表头。
补链表的问题真是捉急。。每次都要错好几回
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 s1 = null;
ListNode s2 = null;
while(head != null){
s2 = head.next;
head.next = s1;
s1 = head;
head = s2;
}
return s1;
}
}