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

How to add lines in this java code?
Java PrintWriter not writing to file [closed]
Gradle Task failing because JSHint detect improvement on code
Generic array creation error on ArrayList [duplicate]
launch java desktop application without eclipse
Android Studio Farsi string
How to remove blank lines in middle of a string Android
Unable to open jar executable in windows
where can we download the oracle.xml.diff package jar. I need to import in a java program of mine
Using Shared preferences to save a string on a button click
How does Android's setOnCLickListener() works?
CRUD Architecture design - deserialization
super constructor in CustomArrayAdapter
How to fix "Cannot resolve 'var' in Spring/Intellij?
How can I perform the unmarshalling on a given xml (reserve engineering)
Returning value to main method in java

Categories

HOME
yii2
hive
oracle11g
layout
hashmap
google-docs
c#-2.0
fancybox
multiple-records
facebook-messenger-bot
gitpitch
gorm
izpack
etl
connection-string
oxyplot
visual-composer
autocad-plugin
icloud-api
nouislider
airconsole
typed.js
uiswipegesturerecognizer
amazon-kinesis-kpl
scaffold
swisscomdev
espeak
serverside-rendering
objectlistview
sqlite2
webdriver-manager
isbn
acoustics
node-sass
angular2-meteor
wso2carbon
dropbox-php
nativeapplication
diagnostics
knockout-components
magma
namecoin
spring-mongodb
dynamics-sl
orthogonal
account-kit
word-vba-mac
visual-c++-2008
azure-virtual-network
mathematica-frontend
ubuntu-10.04
actionbardrawertoggle
suffix-tree
pagerank
freelancer.com-api
prerequisites
lemon
endeca-workbench
jmeter-maven-plugin
kcachegrind
tarjans-algorithm
wyam
php-ci
sniffer
oberon
geonetwork
formatjs
mdt
ceil
system.net.webexception
ms-project-server-2010
blending
coverflow
eventual-consistency
padarn
type-equivalence
smtp-auth
drools-planner
clipper
browser-detection
first-responder
paster
nintendo-ds
photoshop-cs4
data-driven
virtual-functions
pascal-fc
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App