牛客网算法题:反转链表



public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null) {
            return null;
        }
        ListNode newHead=null;
        ListNode cur=head;
         
        ListNode pre=null;
        while(cur!=null) {
            ListNode curNext=cur.next;
            if(curNext==null) {
                newHead=cur;
            }
            cur.next=pre;
            pre=cur;
            cur=curNext;
        }
        return newHead;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42228950/article/details/109479552