Why does this linked list traversal work? How are the references working?Is Java “pass-by-reference” or “pass-by-value”?What is the difference between a variable, object, and reference?How does the Java 'for each' loop work?Why does Java have transient fields?How to detect a loop in a linked list?How to reference a method in javadoc?Writing linked list functionsWhy does this code using random strings print “hello world”?how to get head of the list after traversal in Linked lists?Traversal In linked listI don't understand the implementation of inserting a new node in linked listWhy I can't traverse this linked list in C?
Delete n lines skip 1 line script
How to identify whether a publisher is genuine or not?
What action is recommended if your accommodation refuses to let you leave without paying additional fees?
Is it good to engage in exceptional cases where it is permissible to do a typically forbidden action to which one has a taivah for
Why are the wings of some modern gliders tadpole shaped?
How to say "respectively" in German when listing (enumerating) things
Knights and Knaves: What does C say?
How to add the real hostname in the beginning of Linux cli command
What would happen if I build a half bath without permits?
Detail vs. filler
Verb ending in -ん with positive meaning?
How do I introduce dark themes?
Why is STARTTLS used when it can be downgraded very easily?
How does Monks' Improved Unarmored Movement work out of combat?
Duck, duck, gone!
Convert a string of digits from words to an integer
Was the ruling that prorogation was unlawful only possible because of the creation of a separate supreme court?
How to bring home documents from work?
Looking for circuit board material that can be dissolved
Beyond Futuristic Technology for an Alien Warship?
I transpose the source code, you transpose the input!
Would a horse be sufficient buffer to prevent injury when falling from a great height?
Why isn't there armor to protect from spells in the Potterverse?
French license plates
Why does this linked list traversal work? How are the references working?
Is Java “pass-by-reference” or “pass-by-value”?What is the difference between a variable, object, and reference?How does the Java 'for each' loop work?Why does Java have transient fields?How to detect a loop in a linked list?How to reference a method in javadoc?Writing linked list functionsWhy does this code using random strings print “hello world”?how to get head of the list after traversal in Linked lists?Traversal In linked listI don't understand the implementation of inserting a new node in linked listWhy I can't traverse this linked list in C?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.
Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?
Note: I know this code doesnt handle edge cases. like null nodes, etc...
public class LLnode
int value;
LLnode next;
public LLnode(int x)
this.value = x;
this.next = NULL;
/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?
front.next = front.next.next; //why DOES this physically change the LL ?
public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);
//assignments
c.next = d;
b.next = c;
a.next = b;
removeNode(a,30);
Thanks.
java linked-list
add a comment
|
In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.
Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?
Note: I know this code doesnt handle edge cases. like null nodes, etc...
public class LLnode
int value;
LLnode next;
public LLnode(int x)
this.value = x;
this.next = NULL;
/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?
front.next = front.next.next; //why DOES this physically change the LL ?
public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);
//assignments
c.next = d;
b.next = c;
a.next = b;
removeNode(a,30);
Thanks.
java linked-list
front = front.next;
- that's just changing a local variablefront
so that the value is the current value ofvalue.next
. Compare that withfront.next = front.next.next;
which modifies the object referred to by the current value offront
. You might find this question useful: stackoverflow.com/questions/32010172/…
– Jon Skeet
Mar 28 at 20:10
add a comment
|
In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.
Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?
Note: I know this code doesnt handle edge cases. like null nodes, etc...
public class LLnode
int value;
LLnode next;
public LLnode(int x)
this.value = x;
this.next = NULL;
/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?
front.next = front.next.next; //why DOES this physically change the LL ?
public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);
//assignments
c.next = d;
b.next = c;
a.next = b;
removeNode(a,30);
Thanks.
java linked-list
In C this concept becomes beautifully clear with pointers but im having trouble understanding what exactly is going on here in java.
Can someone please explain to me how come when i traverse the list in removeNode(), it doesnt change any thing on the original object BUT when i do front.next = front.next.next it actually changes the object. Its driving me crazy cause in C i can just use pointers to edit w/e i want. What exactly is going on here with the references?
Note: I know this code doesnt handle edge cases. like null nodes, etc...
public class LLnode
int value;
LLnode next;
public LLnode(int x)
this.value = x;
this.next = NULL;
/*
* This fn removes the node with the specified value n from the linked list
*/
public void removeNode(LLnode head, int n)
LLnode front = head;
while (front.next.value != n)
front = front.next; //why DOESN'T this physically change the LL?
front.next = front.next.next; //why DOES this physically change the LL ?
public static void main(String[] args)
//node creation
LLnode a = new LLnode(10);
LLnode b = new LLnode(20);
LLnode c = new LLnode(30);
LLnode d = new LLnode(40);
//assignments
c.next = d;
b.next = c;
a.next = b;
removeNode(a,30);
Thanks.
java linked-list
java linked-list
asked Mar 28 at 20:07
shockwave88shockwave88
82 bronze badges
82 bronze badges
front = front.next;
- that's just changing a local variablefront
so that the value is the current value ofvalue.next
. Compare that withfront.next = front.next.next;
which modifies the object referred to by the current value offront
. You might find this question useful: stackoverflow.com/questions/32010172/…
– Jon Skeet
Mar 28 at 20:10
add a comment
|
front = front.next;
- that's just changing a local variablefront
so that the value is the current value ofvalue.next
. Compare that withfront.next = front.next.next;
which modifies the object referred to by the current value offront
. You might find this question useful: stackoverflow.com/questions/32010172/…
– Jon Skeet
Mar 28 at 20:10
front = front.next;
- that's just changing a local variable front
so that the value is the current value of value.next
. Compare that with front.next = front.next.next;
which modifies the object referred to by the current value of front
. You might find this question useful: stackoverflow.com/questions/32010172/…– Jon Skeet
Mar 28 at 20:10
front = front.next;
- that's just changing a local variable front
so that the value is the current value of value.next
. Compare that with front.next = front.next.next;
which modifies the object referred to by the current value of front
. You might find this question useful: stackoverflow.com/questions/32010172/…– Jon Skeet
Mar 28 at 20:10
add a comment
|
1 Answer
1
active
oldest
votes
Java is pass-by-value. front = head
copies the reference value from head
into front
. Because of that front = front.next
has no effect on the head
. In the loop front
is created only to point into current element, it's not used to maintain the list.
However front.next = front.next.next
changes the next
field in an object referenced by front
. There is no reference copy for next
field here, like previously front
was a copy of head
.
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406044%2fwhy-does-this-linked-list-traversal-work-how-are-the-references-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Java is pass-by-value. front = head
copies the reference value from head
into front
. Because of that front = front.next
has no effect on the head
. In the loop front
is created only to point into current element, it's not used to maintain the list.
However front.next = front.next.next
changes the next
field in an object referenced by front
. There is no reference copy for next
field here, like previously front
was a copy of head
.
add a comment
|
Java is pass-by-value. front = head
copies the reference value from head
into front
. Because of that front = front.next
has no effect on the head
. In the loop front
is created only to point into current element, it's not used to maintain the list.
However front.next = front.next.next
changes the next
field in an object referenced by front
. There is no reference copy for next
field here, like previously front
was a copy of head
.
add a comment
|
Java is pass-by-value. front = head
copies the reference value from head
into front
. Because of that front = front.next
has no effect on the head
. In the loop front
is created only to point into current element, it's not used to maintain the list.
However front.next = front.next.next
changes the next
field in an object referenced by front
. There is no reference copy for next
field here, like previously front
was a copy of head
.
Java is pass-by-value. front = head
copies the reference value from head
into front
. Because of that front = front.next
has no effect on the head
. In the loop front
is created only to point into current element, it's not used to maintain the list.
However front.next = front.next.next
changes the next
field in an object referenced by front
. There is no reference copy for next
field here, like previously front
was a copy of head
.
answered Mar 28 at 20:21
Karol DowbeckiKarol Dowbecki
30.3k9 gold badges43 silver badges63 bronze badges
30.3k9 gold badges43 silver badges63 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406044%2fwhy-does-this-linked-list-traversal-work-how-are-the-references-working%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
front = front.next;
- that's just changing a local variablefront
so that the value is the current value ofvalue.next
. Compare that withfront.next = front.next.next;
which modifies the object referred to by the current value offront
. You might find this question useful: stackoverflow.com/questions/32010172/…– Jon Skeet
Mar 28 at 20:10