Message from @Jacob
Discord ID: 506665119598510080
Do you think I'm understanding this correctly?
```java
/**
* This method reverse2() will reverse all data nodes in this list, WITHOUT
* creating(introducing) new list nodes, by simply re-wiring the next reference in
* the existing list node. For example, list1 = []ā>[A]ā>[B],
* the reversed list1 will be []-->[B]-->[A],
* after assigning node A to B's next reference and setting A's next to null.
*
*/
public void reverse2() {
if(this.size <= 1)
return;
// The following method call works on a *sublist* without a Dummy Node.
// Namely, we preserved the OLD dummy head node in the reversed list.
this.head.next = reverse(this.head.next, this.head.next.next);
}
/**
* Please implement the helper method below for reverse2().
* @param first, the first node of the list to be reversed.
* @param second, the second node of the list to be reversed.
* @return the new head node of the reversed list.
* Note: you are NOT allowed to create new list node, but have to
* re-wiring the existing nodes by changing their next references.
* Write this method using recursion.
*/
private ListNode reverse(ListNode first, ListNode second) {
ListNode newHead = new ListNode();
if (second.next != null) {
ListNode curr = first;
ListNode newFirstNode = null;
ListNode newLastNode = null;
while (curr.next != null) {
newLastNode = curr;
newFirstNode = curr.next;
curr = curr.next.next;
}
newHead.next = newFirstNode;
newLastNode.next = null;
newFirstNode.next = reverse(newFirstNode.next, newFirstNode.next.next);
}
else {
newHead.next = second;
second.next = first;
first.next = null;
}
return newHead; //change this line of code as needed.
}
```
@ThisIsChris okay this is what I have right now, do you think this works?
<@&435155896780324864> In the past week I've been hit up by recruiters 3 times asking if I know how to update a website for Search Engine Optimization (SEO). I don't intend to go into it, but the demand is there and I know this is a field you can get into relatively quickly. Lots of information online to learn from this is a big one https://moz.com/beginners-guide-to-seo
Iām an SEO and Iād love to pick up work if I could. If you need help learning about it or see an offer let me know š
@Gumbo - AZ will do!
<@&435155896780324864> Does anyone know how to do a Big O time complexity analysis on a Java function?
@Jacob example function?
```java
public static void two(int n)
{
if(n > 0)
{
System.out.println("n: " +n);
two(n - 1);
two(n - 1);
}
else if (n < 0)
{
two(n + 1);
two(n + 1);
System.out.println(ā³n: ā³ + n);
}
}
```
It's mostly like doing it on paper, you have most of the same assumptions of the Random Access Machine
Do you have any skepticism that assumptions of RAM don't hold?
No, it's just a basic analysis. I need to provide a "logical justification" for my answer. Not sure if that means I have to write a paragraph or there's some kind of equation I need to use.
There's an equation.
Not necessarily expecting an answer to the question from anyone, but maybe something that can get me in the right direction
f(0) = O(1)
f(|n|) = 2*f(|n|-1) + O(1)
š¬
so f(n) = O(2^n)
Is that the equation for this particular function?
yep
So start by looking at the function to understand what is the "base case". The base case is the input that doesn't cause a recursive call
ah, okay
and write the base case as one of those formulas?
the only input that doesn't have a recursive call is n=0
yep
so how do I move up from there?
So after that you really have two cases, one when n is > 0 and one when , is < 0. If you look closely what you have in each case is that n decreases or increases towards 0 with steps of 1. I'm just here pointing out the two cases. For simplicity consider what happens in just the positive case first
so for the positive case you have that f(n) involves a check on n, which is constant time, a printline which is constant time, and then two calls to f(n-1)
so in this n positive case
f(n) = the constant stuff + 2\*f(n-1) = O(1) + 2\*f(n-1)
But don't the calls to f(n-1) create even more calls?
since it's recursive
Yep
Do I need some kind of formula to determine how many calls it will create?
so by using the same formula, f(n-1) = 2*f(n-2) + o(1)
plugging that in to what you had before:
f(n) = 2\*(2\*f(n-2) + O(1)) + O(1) = 2\*2\*f(n-2) + O(1)
ah, so it just simplifies to another logarithmic function?
yes
its exponential (the inverse of logarithm)
Okay. That should be enough to write up a logical explanation, I think. Apparently the other two function are easier to analyze.
Thanks a lot
```java
public void three(int n)
{
int i, j, k;
for (i = n/2; i > 0; i = i/2)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++)
System.out.println("i: " + i + " j: " + j+" k: " + k);
```