From charlesreid1

(Created page with "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 po...")
 
No edit summary
Line 32: Line 32:
list.head = runner
list.head = runner
</pre>
</pre>
=Flags=
{{DataStructuresFlag}}
[[Category:Java]]
[[Category:Linked Lists]]

Revision as of 05:18, 5 June 2017

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

Flags