Saturday, August 27, 2011

Inverting a Linked List

We can invert a linked list in place using three pointers:

procedure INVERT(X)
p := X, q := 0
while p \= 0 do
r := q;
q:= p;
p := p.link
q.link := r
end X := q
end INVERT

No comments:

Post a Comment