Skip to content

Commit 4827704

Browse files
author
Cody Gagnon
committed
Node p = head
/** * Returns the element at the specified position in this list. */
1 parent 3ccea79 commit 4827704

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎class_code/11-15-Linked-Lists/MyLinkedList.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,24 @@ public E get(int index) {
103103
* Returns the index of the specified element (-1 if there is no match)
104104
*/
105105
public int indexOf(Object o) {
106-
107-
// If o is null
108-
if (o == null) // look for a null element in the list
109-
{
110-
} else // o is an object (use equals)
111-
{
106+
Node p = head;
107+
for (int i = 0; i < size; i++) {
108+
// If o is null
109+
if (o == null) // look for a null element in the list
110+
{
111+
if (p.item == null) {
112+
return i;
113+
}
114+
} else { // o is an object (use equals)
115+
if (o.equals(p.item)) {
116+
return i;
117+
}
118+
}
119+
112120
}
113121

114122
// if we get here, o is not in the list
123+
return -1;
115124
}
116125

117126
/**

0 commit comments

Comments
 (0)