Why does my solution exceed time limit ?


  • 0
    K

    public ListNode reverseList(ListNode head) {
    if(head==null||head.next==null) return head;
    ListNode last=head;
    ListNode now=head.next;
    while (now.next!=null){
    now.next=last;
    last=last.next;
    now=now.next;
    }
    now.next=last;
    head.next=null;
    return now;
    }

    it is definitely O(n), why?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.