反转链表 --剑指offer

输入一个链表,反转链表后,输出新链表的表头。

比较经典 也比较容易理解 画一下图跟着代码走一下 就明白了

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode l1=null,l2=null;
        while(head != null){
            l2=head.next;
            head.next=l1;
            l1=head;
            head=l2;
        }
        return l1; 
    }
}
原文地址:https://www.cnblogs.com/nlw-blog/p/12421610.html