Understanding logic with two pointers in a linked list C# (Not sure how to ask this…) [closed]
Sorry if this isn't appropriate, but I'm really try to work on my algorithm skills.
static public ListNode RemoveNthNodeFromEndOf(ListNode head, int n)
{
if (head == null || n == 0) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy , q = dummy.next;
int i = 0;
while (q.next != null)
{
if (i >= n - 1) p = p.next;
q = q.next;
i++;
}
p.next = p.next.next;
return dummy.next;
}
I completely understand what they did here, I just don't understand how you get to that answer and I would honestly like to figure out that skill. Basically, how do you approach this and think, i >= n-1 then p should start to follow q. Steps to the thought to get there I guess? Not
c# algorithm
closed as primarily opinion-based by Kobi, Alexei Levenkov, AdrianHHH, pinkfloydx33, ggorlen Dec 29 '18 at 16:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
Sorry if this isn't appropriate, but I'm really try to work on my algorithm skills.
static public ListNode RemoveNthNodeFromEndOf(ListNode head, int n)
{
if (head == null || n == 0) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy , q = dummy.next;
int i = 0;
while (q.next != null)
{
if (i >= n - 1) p = p.next;
q = q.next;
i++;
}
p.next = p.next.next;
return dummy.next;
}
I completely understand what they did here, I just don't understand how you get to that answer and I would honestly like to figure out that skill. Basically, how do you approach this and think, i >= n-1 then p should start to follow q. Steps to the thought to get there I guess? Not
c# algorithm
closed as primarily opinion-based by Kobi, Alexei Levenkov, AdrianHHH, pinkfloydx33, ggorlen Dec 29 '18 at 16:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
There has to be a way to mathematically write this out before you jump into it? I'm so used to my loops going forward.. Not sure how to approach different directions? or spacing? or .. something! wish I knew how to ask this..
– Kathrine Stack
Dec 29 '18 at 6:07
I completely understand what they did here, I just don't understand how you get to that answer
are you saying you just dont understand how the code achieves this?
– TheGeneral
Dec 29 '18 at 6:16
Hello Kathrine. In my experience a good first step for (relatively simple) algorithmic question is to try and solve example problems on a piece of paper, using the same constraints you'd have in code - you cannot go backwards on a lined list, so you need some way of "remembering" where you were. I think this is a good first step that will lead to "is my position n places ahead of this pointer". That said - I'm voting to close the question as opinion based - each person thinks in a different way, a general process to solve such problems is off-topic anyway (if it exists).
– Kobi
Dec 29 '18 at 6:17
Gotcha. Fair. I guess that's why I tagged it as algorithm. Thought that would allow for this. But @TheGeneral , I understand how it achieves it, I'm just looking for ways to get to a solution like that. What are steps to solving a question like this/what questions are you trying to answer
– Kathrine Stack
Dec 29 '18 at 6:24
1
The question you seem to be asking is way too broad for SO. Get any good "prepare to coding interview" book and it will have good section on approaches (i.e. amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/…) .
– Alexei Levenkov
Dec 29 '18 at 6:39
add a comment |
Sorry if this isn't appropriate, but I'm really try to work on my algorithm skills.
static public ListNode RemoveNthNodeFromEndOf(ListNode head, int n)
{
if (head == null || n == 0) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy , q = dummy.next;
int i = 0;
while (q.next != null)
{
if (i >= n - 1) p = p.next;
q = q.next;
i++;
}
p.next = p.next.next;
return dummy.next;
}
I completely understand what they did here, I just don't understand how you get to that answer and I would honestly like to figure out that skill. Basically, how do you approach this and think, i >= n-1 then p should start to follow q. Steps to the thought to get there I guess? Not
c# algorithm
Sorry if this isn't appropriate, but I'm really try to work on my algorithm skills.
static public ListNode RemoveNthNodeFromEndOf(ListNode head, int n)
{
if (head == null || n == 0) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy , q = dummy.next;
int i = 0;
while (q.next != null)
{
if (i >= n - 1) p = p.next;
q = q.next;
i++;
}
p.next = p.next.next;
return dummy.next;
}
I completely understand what they did here, I just don't understand how you get to that answer and I would honestly like to figure out that skill. Basically, how do you approach this and think, i >= n-1 then p should start to follow q. Steps to the thought to get there I guess? Not
c# algorithm
c# algorithm
edited Dec 29 '18 at 6:01
Backs
19k33158
19k33158
asked Dec 29 '18 at 5:59
Kathrine StackKathrine Stack
295
295
closed as primarily opinion-based by Kobi, Alexei Levenkov, AdrianHHH, pinkfloydx33, ggorlen Dec 29 '18 at 16:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by Kobi, Alexei Levenkov, AdrianHHH, pinkfloydx33, ggorlen Dec 29 '18 at 16:36
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
There has to be a way to mathematically write this out before you jump into it? I'm so used to my loops going forward.. Not sure how to approach different directions? or spacing? or .. something! wish I knew how to ask this..
– Kathrine Stack
Dec 29 '18 at 6:07
I completely understand what they did here, I just don't understand how you get to that answer
are you saying you just dont understand how the code achieves this?
– TheGeneral
Dec 29 '18 at 6:16
Hello Kathrine. In my experience a good first step for (relatively simple) algorithmic question is to try and solve example problems on a piece of paper, using the same constraints you'd have in code - you cannot go backwards on a lined list, so you need some way of "remembering" where you were. I think this is a good first step that will lead to "is my position n places ahead of this pointer". That said - I'm voting to close the question as opinion based - each person thinks in a different way, a general process to solve such problems is off-topic anyway (if it exists).
– Kobi
Dec 29 '18 at 6:17
Gotcha. Fair. I guess that's why I tagged it as algorithm. Thought that would allow for this. But @TheGeneral , I understand how it achieves it, I'm just looking for ways to get to a solution like that. What are steps to solving a question like this/what questions are you trying to answer
– Kathrine Stack
Dec 29 '18 at 6:24
1
The question you seem to be asking is way too broad for SO. Get any good "prepare to coding interview" book and it will have good section on approaches (i.e. amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/…) .
– Alexei Levenkov
Dec 29 '18 at 6:39
add a comment |
There has to be a way to mathematically write this out before you jump into it? I'm so used to my loops going forward.. Not sure how to approach different directions? or spacing? or .. something! wish I knew how to ask this..
– Kathrine Stack
Dec 29 '18 at 6:07
I completely understand what they did here, I just don't understand how you get to that answer
are you saying you just dont understand how the code achieves this?
– TheGeneral
Dec 29 '18 at 6:16
Hello Kathrine. In my experience a good first step for (relatively simple) algorithmic question is to try and solve example problems on a piece of paper, using the same constraints you'd have in code - you cannot go backwards on a lined list, so you need some way of "remembering" where you were. I think this is a good first step that will lead to "is my position n places ahead of this pointer". That said - I'm voting to close the question as opinion based - each person thinks in a different way, a general process to solve such problems is off-topic anyway (if it exists).
– Kobi
Dec 29 '18 at 6:17
Gotcha. Fair. I guess that's why I tagged it as algorithm. Thought that would allow for this. But @TheGeneral , I understand how it achieves it, I'm just looking for ways to get to a solution like that. What are steps to solving a question like this/what questions are you trying to answer
– Kathrine Stack
Dec 29 '18 at 6:24
1
The question you seem to be asking is way too broad for SO. Get any good "prepare to coding interview" book and it will have good section on approaches (i.e. amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/…) .
– Alexei Levenkov
Dec 29 '18 at 6:39
There has to be a way to mathematically write this out before you jump into it? I'm so used to my loops going forward.. Not sure how to approach different directions? or spacing? or .. something! wish I knew how to ask this..
– Kathrine Stack
Dec 29 '18 at 6:07
There has to be a way to mathematically write this out before you jump into it? I'm so used to my loops going forward.. Not sure how to approach different directions? or spacing? or .. something! wish I knew how to ask this..
– Kathrine Stack
Dec 29 '18 at 6:07
I completely understand what they did here, I just don't understand how you get to that answer
are you saying you just dont understand how the code achieves this?– TheGeneral
Dec 29 '18 at 6:16
I completely understand what they did here, I just don't understand how you get to that answer
are you saying you just dont understand how the code achieves this?– TheGeneral
Dec 29 '18 at 6:16
Hello Kathrine. In my experience a good first step for (relatively simple) algorithmic question is to try and solve example problems on a piece of paper, using the same constraints you'd have in code - you cannot go backwards on a lined list, so you need some way of "remembering" where you were. I think this is a good first step that will lead to "is my position n places ahead of this pointer". That said - I'm voting to close the question as opinion based - each person thinks in a different way, a general process to solve such problems is off-topic anyway (if it exists).
– Kobi
Dec 29 '18 at 6:17
Hello Kathrine. In my experience a good first step for (relatively simple) algorithmic question is to try and solve example problems on a piece of paper, using the same constraints you'd have in code - you cannot go backwards on a lined list, so you need some way of "remembering" where you were. I think this is a good first step that will lead to "is my position n places ahead of this pointer". That said - I'm voting to close the question as opinion based - each person thinks in a different way, a general process to solve such problems is off-topic anyway (if it exists).
– Kobi
Dec 29 '18 at 6:17
Gotcha. Fair. I guess that's why I tagged it as algorithm. Thought that would allow for this. But @TheGeneral , I understand how it achieves it, I'm just looking for ways to get to a solution like that. What are steps to solving a question like this/what questions are you trying to answer
– Kathrine Stack
Dec 29 '18 at 6:24
Gotcha. Fair. I guess that's why I tagged it as algorithm. Thought that would allow for this. But @TheGeneral , I understand how it achieves it, I'm just looking for ways to get to a solution like that. What are steps to solving a question like this/what questions are you trying to answer
– Kathrine Stack
Dec 29 '18 at 6:24
1
1
The question you seem to be asking is way too broad for SO. Get any good "prepare to coding interview" book and it will have good section on approaches (i.e. amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/…) .
– Alexei Levenkov
Dec 29 '18 at 6:39
The question you seem to be asking is way too broad for SO. Get any good "prepare to coding interview" book and it will have good section on approaches (i.e. amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/…) .
– Alexei Levenkov
Dec 29 '18 at 6:39
add a comment |
1 Answer
1
active
oldest
votes
You want a pointer n
from the end. But you can't move backwards, and you can't see forwards. What to do?
Suppose you're in a dark corridor and someone tells you the light switch is three meters from the end. It's too dark to see three meters, so you pick up the proverbial ten-foot pole, hold it pointing forward, and walk down the corridor until the pole touches the end.
Here, we don't have a pole handy, but we can build one by making a second pointer and stepping it n
times. Now, we have two pointers n
steps apart and we can advance both in parallel until the advance pointer hits the wallend of the list. Then the other pointer is n
steps from the end.
How to come up with these solutions? Both come about from the same thought process: "I could do this if only I had something walking ahead of me." I guess practice helps. Really wanting to solve the problem helps, as does believing the problem has a solution. Add a bit of creativity and maybe a bit of seeing the world as you would like it to be. In short, the same way you solve any problem.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want a pointer n
from the end. But you can't move backwards, and you can't see forwards. What to do?
Suppose you're in a dark corridor and someone tells you the light switch is three meters from the end. It's too dark to see three meters, so you pick up the proverbial ten-foot pole, hold it pointing forward, and walk down the corridor until the pole touches the end.
Here, we don't have a pole handy, but we can build one by making a second pointer and stepping it n
times. Now, we have two pointers n
steps apart and we can advance both in parallel until the advance pointer hits the wallend of the list. Then the other pointer is n
steps from the end.
How to come up with these solutions? Both come about from the same thought process: "I could do this if only I had something walking ahead of me." I guess practice helps. Really wanting to solve the problem helps, as does believing the problem has a solution. Add a bit of creativity and maybe a bit of seeing the world as you would like it to be. In short, the same way you solve any problem.
add a comment |
You want a pointer n
from the end. But you can't move backwards, and you can't see forwards. What to do?
Suppose you're in a dark corridor and someone tells you the light switch is three meters from the end. It's too dark to see three meters, so you pick up the proverbial ten-foot pole, hold it pointing forward, and walk down the corridor until the pole touches the end.
Here, we don't have a pole handy, but we can build one by making a second pointer and stepping it n
times. Now, we have two pointers n
steps apart and we can advance both in parallel until the advance pointer hits the wallend of the list. Then the other pointer is n
steps from the end.
How to come up with these solutions? Both come about from the same thought process: "I could do this if only I had something walking ahead of me." I guess practice helps. Really wanting to solve the problem helps, as does believing the problem has a solution. Add a bit of creativity and maybe a bit of seeing the world as you would like it to be. In short, the same way you solve any problem.
add a comment |
You want a pointer n
from the end. But you can't move backwards, and you can't see forwards. What to do?
Suppose you're in a dark corridor and someone tells you the light switch is three meters from the end. It's too dark to see three meters, so you pick up the proverbial ten-foot pole, hold it pointing forward, and walk down the corridor until the pole touches the end.
Here, we don't have a pole handy, but we can build one by making a second pointer and stepping it n
times. Now, we have two pointers n
steps apart and we can advance both in parallel until the advance pointer hits the wallend of the list. Then the other pointer is n
steps from the end.
How to come up with these solutions? Both come about from the same thought process: "I could do this if only I had something walking ahead of me." I guess practice helps. Really wanting to solve the problem helps, as does believing the problem has a solution. Add a bit of creativity and maybe a bit of seeing the world as you would like it to be. In short, the same way you solve any problem.
You want a pointer n
from the end. But you can't move backwards, and you can't see forwards. What to do?
Suppose you're in a dark corridor and someone tells you the light switch is three meters from the end. It's too dark to see three meters, so you pick up the proverbial ten-foot pole, hold it pointing forward, and walk down the corridor until the pole touches the end.
Here, we don't have a pole handy, but we can build one by making a second pointer and stepping it n
times. Now, we have two pointers n
steps apart and we can advance both in parallel until the advance pointer hits the wallend of the list. Then the other pointer is n
steps from the end.
How to come up with these solutions? Both come about from the same thought process: "I could do this if only I had something walking ahead of me." I guess practice helps. Really wanting to solve the problem helps, as does believing the problem has a solution. Add a bit of creativity and maybe a bit of seeing the world as you would like it to be. In short, the same way you solve any problem.
edited Dec 29 '18 at 13:15
answered Dec 29 '18 at 6:55
ricirici
153k19135200
153k19135200
add a comment |
add a comment |
There has to be a way to mathematically write this out before you jump into it? I'm so used to my loops going forward.. Not sure how to approach different directions? or spacing? or .. something! wish I knew how to ask this..
– Kathrine Stack
Dec 29 '18 at 6:07
I completely understand what they did here, I just don't understand how you get to that answer
are you saying you just dont understand how the code achieves this?– TheGeneral
Dec 29 '18 at 6:16
Hello Kathrine. In my experience a good first step for (relatively simple) algorithmic question is to try and solve example problems on a piece of paper, using the same constraints you'd have in code - you cannot go backwards on a lined list, so you need some way of "remembering" where you were. I think this is a good first step that will lead to "is my position n places ahead of this pointer". That said - I'm voting to close the question as opinion based - each person thinks in a different way, a general process to solve such problems is off-topic anyway (if it exists).
– Kobi
Dec 29 '18 at 6:17
Gotcha. Fair. I guess that's why I tagged it as algorithm. Thought that would allow for this. But @TheGeneral , I understand how it achieves it, I'm just looking for ways to get to a solution like that. What are steps to solving a question like this/what questions are you trying to answer
– Kathrine Stack
Dec 29 '18 at 6:24
1
The question you seem to be asking is way too broad for SO. Get any good "prepare to coding interview" book and it will have good section on approaches (i.e. amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/…) .
– Alexei Levenkov
Dec 29 '18 at 6:39