JZ24 反转链表
NC78 反转链表
//11
//迭代
import java.util.*;
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode prev=null;
ListNode cur=head;
while(cur!=null){
ListNode next=cur.next;
cur.next=prev;
prev=cur;
cur=next;
}
return prev;
}
}
// //递归?
// import java.util.*;
// public class Solution {
// public ListNode ReverseList(ListNode head) {
// //递归结束条件
// if(head==null||head.next==null){
// return head;
// }
// ListNode vhead=ReverseList(head.next);
// //逆转本级节点
// head.next.next=head;
// //尾节点置空
// head.next=null;
// return vhead;
// }
// }