反转链表最精简写法

  public ListNode reverseList(ListNode head){
        ListNode res=null;
        ListNode next=null;
        while (head!=null){
            //记录下个位置
            next=head.next;
            //将这个连接进来
            head.next=res;
            res=head;
            //将head指向它本该指向得到下一位
            head=next;
        }
        return res;
    }

 

posted @ 2020-09-13 14:32  程序杰杰  阅读(120)  评论(0编辑  收藏  举报