# 2 Java AC Solutions: Iterative and Recursive

• Iterative Solution:

1. Split the list into two halfs
2. Reverse the second half
3. Compare the first half and second half
``````public class Solution {
return true;
}

preMid.next = null;

return false;
}

}

return true;
}

ListNode pre = null;

while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}

return pre;
}

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}

return slow;
}
}
``````

Recursive Solution:

``````public class Solution {
ListNode cur;

}

return true;
}

if (flag && head.val == cur.val) {
cur = cur.next;
return true;
}

return false;
}
}``````

• You can't reverse using recursion because it must be O(1) space. Using recursion to reverse the second half will consume O(n/2) space.

• I didn't reverse the list using recursion. The first solution is iterative

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