java


Moving backwards in a singly linked list?


I'm creating my first linked list and while I understand doubly linked lists are really for going backwards I am attempting to create a method that moves the current node backwards in the list by one node in a SINGLY linked list.
Here's what I got so far, I've included my go to next for reference and constructors:
//Paramaterized construct
public ListNode(int aData, ListNode aLink) {
this.data= aData;
this.link = aLink;
}
}
private ListNode head; //First element
private ListNode current; //Current node of interest
private ListNode previous; //Node behind current
public void goToNext () {
previous = current;
current = current.link;
}
//TODO: Fix previous
public void goToPrev () {
if (current != head) {
}
else
System.out.println("Current node is the head, sorry");
I forgot to add my private ListNode class:
private class ListNode
{
private int data;
private ListNode link;
//Default construct
public ListNode ()
{
this.data = data;
this.link = link;
}
//Paramaterized construct
public ListNode(int aData, ListNode aLink)
{
this.data= aData;
this.link = aLink;
}
}
I'm thinking I have to iterate from the start of the list until I find the node that has next equal to my current node. But I'm not sure how to exactly set up that while loop and have the correct body.
To do this you need to iterate over the list with TWO pointers, not one. The trick is to have one ahead of the other by exactly one node, so when pointer #1 finds the node of interest, pointer #2 is the node behind it in the list and you can "move backwards" by accessing #2.
You can not iterate singly linked list backwards. If you want to iterate backwards then you should you doubly linked list. Or you can achieve this take ref of the node before iterate.

Related Links

when the thread will release the lock on object?
Background JSwing Image Loading Oddly
If not working properly [duplicate]
Apache Commons Configurations containsKey returns false on existing key
Implementing express checkout (PayPal) using SOAP
Can't replace one file with other
Android Studio SDK path error
Cannot come up with idea of practice example with RxJava (reactive programming)
Swagger JAX-RS with Jersey 1.19 gives 'Conflicting URI templates' error
How to poll a URL using the same InputStreamReader
How to Parse XML file via XPATH
JAVA - String index out of range, I can't figure out why
compiling and running through a command line [closed]
Double Accuracy
Why is my android view's width 0?
How do I show a notification in IntelliJ?

Categories

HOME
compiler-construction
xamarin
multithreading
client
date
include
iot
filtering
twitter-bootstrap-4
survey
umd
datastax-java-driver
android-youtube-api
facebook-messenger-bot
applepay
slurm
cloudkit
maude-system
offline
jsprit
ghc
finite-automata
jsdoc
pugjs
sms-gateway
shared-hosting
plunker
wkwebview
trading
h2db
filezilla
web-api-testing
applozic
jaxb2
srcset
scorm2004
fog
copying
catch-all
usbserial
openoffice.org
form-data
android-kernel
mozilla
geopositioning
ghost4j
http-referer
y86
plsql-psp
hot-module-replacement
serverside-rendering
unobtrusive-validation
lumberjack
fedex
reportingservices-2005
turbogears
flashair
wptoolkit
auto-update
hibernate-tools
komodoedit
return-value
gabor-filter
capacity
eventkit
google-cdn
angular-strap
static-ip-address
android-fonts
ado.net-entity-data-model
azure-virtual-network
mikroc
contact-list
lua-5.1
qgraphicsview
bgp
cartesian-product
t4mvc
retina
tmuxinator
wordml
citrus-pay
iis-arr
drawbitmap
cakephp-3.1
dereference
xc16
google-reader
responsive-slides
typeof
dml
meteor-velocity
terminal-services
google-cloud-save
jplaton
http-unit
cisco-jtapi
labwindows
assembly-loading
whoosh
data-loss
spyware
pyinotify
fluent-interface
suppress
ext3
virtual-functions
temporal-database
thread-local-storage
zune

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App