Find middle element of LinkedList [closed]
Clash 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);
}
java linked-list
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.
add a comment |Â
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);
}
java linked-list
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 uncheckedfastCursor.next.next
). Please edit the question to include your test set.
â Toby Speight
Feb 8 at 13:24
add a comment |Â
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);
}
java linked-list
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);
}
java linked-list
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 uncheckedfastCursor.next.next
). Please edit the question to include your test set.
â Toby Speight
Feb 8 at 13:24
add a comment |Â
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 uncheckedfastCursor.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
add a comment |Â
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
fastCursor = fastCursor.next.next;
will crash if the number of nodes in the list is even (but greater than zero).
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
add a comment |Â
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
add a comment |Â
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