Find middle element of LinkedList [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-2
down vote

favorite












 private static String findMiddleElementOfLinkedList(Node head) 
if (head == null)
return null;

Node slowCursor = head;
if (slowCursor.next != null)
Node fastCursor = head.next.next;
while (fastCursor != null)
slowCursor = slowCursor.next;
fastCursor = fastCursor.next.next;



return String.valueOf(slowCursor.data);

}






share|improve this question











closed as unclear what you're asking by Sam Onela, Mast, t3chb0t, Dannnno, Graipher Feb 9 at 10:10


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    there has to be some kind of question here (preferably with some context)
    – Sharon Ben Asher
    Feb 6 at 8:15






  • 1




    It's also completely broken for odd-length lists (due to the unchecked fastCursor.next.next). Please edit the question to include your test set.
    – Toby Speight
    Feb 8 at 13:24

















up vote
-2
down vote

favorite












 private static String findMiddleElementOfLinkedList(Node head) 
if (head == null)
return null;

Node slowCursor = head;
if (slowCursor.next != null)
Node fastCursor = head.next.next;
while (fastCursor != null)
slowCursor = slowCursor.next;
fastCursor = fastCursor.next.next;



return String.valueOf(slowCursor.data);

}






share|improve this question











closed as unclear what you're asking by Sam Onela, Mast, t3chb0t, Dannnno, Graipher Feb 9 at 10:10


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    there has to be some kind of question here (preferably with some context)
    – Sharon Ben Asher
    Feb 6 at 8:15






  • 1




    It's also completely broken for odd-length lists (due to the unchecked fastCursor.next.next). Please edit the question to include your test set.
    – Toby Speight
    Feb 8 at 13:24













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











 private static String findMiddleElementOfLinkedList(Node head) 
if (head == null)
return null;

Node slowCursor = head;
if (slowCursor.next != null)
Node fastCursor = head.next.next;
while (fastCursor != null)
slowCursor = slowCursor.next;
fastCursor = fastCursor.next.next;



return String.valueOf(slowCursor.data);

}






share|improve this question











 private static String findMiddleElementOfLinkedList(Node head) 
if (head == null)
return null;

Node slowCursor = head;
if (slowCursor.next != null)
Node fastCursor = head.next.next;
while (fastCursor != null)
slowCursor = slowCursor.next;
fastCursor = fastCursor.next.next;



return String.valueOf(slowCursor.data);

}








share|improve this question










share|improve this question




share|improve this question









asked Feb 5 at 23:43









TheLearner

400311




400311




closed as unclear what you're asking by Sam Onela, Mast, t3chb0t, Dannnno, Graipher Feb 9 at 10:10


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Sam Onela, Mast, t3chb0t, Dannnno, Graipher Feb 9 at 10:10


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 2




    there has to be some kind of question here (preferably with some context)
    – Sharon Ben Asher
    Feb 6 at 8:15






  • 1




    It's also completely broken for odd-length lists (due to the unchecked fastCursor.next.next). Please edit the question to include your test set.
    – Toby Speight
    Feb 8 at 13:24













  • 2




    there has to be some kind of question here (preferably with some context)
    – Sharon Ben Asher
    Feb 6 at 8:15






  • 1




    It's also completely broken for odd-length lists (due to the unchecked fastCursor.next.next). Please edit the question to include your test set.
    – Toby Speight
    Feb 8 at 13:24








2




2




there has to be some kind of question here (preferably with some context)
– Sharon Ben Asher
Feb 6 at 8:15




there has to be some kind of question here (preferably with some context)
– Sharon Ben Asher
Feb 6 at 8:15




1




1




It's also completely broken for odd-length lists (due to the unchecked fastCursor.next.next). Please edit the question to include your test set.
– Toby Speight
Feb 8 at 13:24





It's also completely broken for odd-length lists (due to the unchecked fastCursor.next.next). Please edit the question to include your test set.
– Toby Speight
Feb 8 at 13:24











1 Answer
1






active

oldest

votes

















up vote
1
down vote













fastCursor = fastCursor.next.next; will crash if the number of nodes in the list is even (but greater than zero).






share|improve this answer





















  • Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
    – TheLearner
    Feb 6 at 3:05







  • 1




    You need to try 4 :)
    – RobAu
    Feb 6 at 8:09

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













fastCursor = fastCursor.next.next; will crash if the number of nodes in the list is even (but greater than zero).






share|improve this answer





















  • Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
    – TheLearner
    Feb 6 at 3:05







  • 1




    You need to try 4 :)
    – RobAu
    Feb 6 at 8:09














up vote
1
down vote













fastCursor = fastCursor.next.next; will crash if the number of nodes in the list is even (but greater than zero).






share|improve this answer





















  • Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
    – TheLearner
    Feb 6 at 3:05







  • 1




    You need to try 4 :)
    – RobAu
    Feb 6 at 8:09












up vote
1
down vote










up vote
1
down vote









fastCursor = fastCursor.next.next; will crash if the number of nodes in the list is even (but greater than zero).






share|improve this answer













fastCursor = fastCursor.next.next; will crash if the number of nodes in the list is even (but greater than zero).







share|improve this answer













share|improve this answer



share|improve this answer











answered Feb 5 at 23:51









200_success

123k14143401




123k14143401











  • Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
    – TheLearner
    Feb 6 at 3:05







  • 1




    You need to try 4 :)
    – RobAu
    Feb 6 at 8:09
















  • Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
    – TheLearner
    Feb 6 at 3:05







  • 1




    You need to try 4 :)
    – RobAu
    Feb 6 at 8:09















Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
– TheLearner
Feb 6 at 3:05





Can you provide me with an example of this case? I tested this code with one node, two nodes and 6 nodes.
– TheLearner
Feb 6 at 3:05





1




1




You need to try 4 :)
– RobAu
Feb 6 at 8:09




You need to try 4 :)
– RobAu
Feb 6 at 8:09


Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?