space O(1) solution or not?


  • 0
    R

    Could someone tell me the space complexity of my code please?
    I think it is O(1), but OJ said Memory Limit Exceeded for the input [1,2,3].

    Thank you so much.

    public class Solution {
       	public ListNode oddEvenList(ListNode head) {
    
    		if (head == null || head.next == null) {
    			return head;
    		}
    
    		ListNode p1 = head;
    		ListNode p2 = head.next;
    		ListNode p3 = head.next;
    
    		while (p2.next != null && p2.next.next != null) {
    			p1.next = p1.next.next;
    			p2.next = p2.next.next;
    
    			p1 = p1.next;
    			p2 = p2.next;
    
    		}
    		if (p2.next != null) {
    			p1.next = p1.next.next;
    			p1 = p1.next;
    		}
    		p1.next = p3;
    
    		return head;
    	}
    }
    

Log in to reply
 

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