Ac solution code


  • 3

    The basic idea is to set current.next = last, and then set current = current.next (current.next should be saved to temp before being modified) to keep the loop:

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

  • 0
    M
    struct ListNode* reverseList(struct ListNode* head){
    if(head->next!=NULL){
        reverseList(head->next)->next=head;
    }
    else{
        return head;
    }
    

    }


Log in to reply
 

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