// linked list reverse
public void reverse() {
// __HEAD_INSERT__
// access all the elements in the oldList from now on //
// p is the oldList[0] at the moment. //
Node p = _head._next;
// clear the real head to prepare for the newList //
_head._next = null;
Node r;
while(p!=null) {
// store the next element (oldList[i+1]) //
r = p._next;
// p becomes the new head of the newList. //
p._next = _head._next;
// p becomes the newList[0] of the newList. //
_head._next = p;
// now let p jump to the next elements in oldList. //
p = r;
}
// __THREE_NODES__
// quite easy ... //
Node p = _head;
Node q = p._next;
Node r = q._next;
while(r!=null) {
q._next = p;
p = q;
q = r;
r = r._next;
}
q._next = p;
_head._next = q;
}
dataStructure notes 01
于 2022-10-16 01:04:09 首次发布