题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

昨天又想到一种方法,用指针去做,时间复杂度:O(链表长度)

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        
        ListNode cur = null;
        ListNode next = null;
        ListNode root = null;
        
        if(head == null || head.next == null){
            return head;
        }
        
        cur = head;
        next = cur.next;
        
        do{
            cur.next = root;
            root = cur;
            cur = next;
            next = next.next;
        }while(next != null);
        
        cur.next = root;
        
        return cur;
        
        
        
    }
}
全部评论

相关推荐

这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
你找工作的时候用AI吗?
点赞 评论 收藏
分享
机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务