last = cur = cur == null ? head : cur.link;
I am trying to understand this using if and else statement. But I am stuck on this one. Can anyone help this out
last = cur = cur == null ? head : cur.link;
I am trying to understand this using if and else statement. But I am stuck on this one. Can anyone help this out
Your expression is equal to
if (cur == null) {
cur = head;
} else {
cur = cur.link;
}
last = cur;
You can split the example into two parts for easier understanding. Part 1:
last = cur = xxx;
is the same as
cur = xxx;
last = cur;
Part 2:
cur = cur == null ? head : cur.link;
is the same as
if (cur == null) {
cur = head;
} else {
cur = cur.link;
}
And part 2 would be even easier to understand if cur
didn't appear three times. So this is simpler:
a = b == null ? c : d;
is the same as
if (b == null) {
a = c;
} else {
a = d;
}