Linked Lists/Java/Reverse
From charlesreid1
To reverse a linked list, need to hang on to three separate pointers:
- One pointer is the runner, which points to the current node being "fixed" (reversed in place).
- One pointer is the prior, which points to the node that will be behind the runner in the list
- One pointer is the temporary pointer to the rest of the list that is still being processed
if(size=0):
return
if(size==1)
return
if size=2)
runner = head
prior = null
temp = runner.getNext()
runner.setNext(prior)
if length is 0, do nothing
if length is 1, do nothing
runner = head
prev = null
next runner = head.getNext()
while(nextrunner != null)
runner.setNext(prev)
prev = runner
runner = nextrunner
nextrunner = runner.getNext()
list.head = runner