Java Reference
In-Depth Information
public void deleteHead() {
if (head != null) head = head.next;
}
The method
addTail
adds a new node at the end of the list. It finds the last node (for which
next
is
null
) and sets
it to point to the new node.
public void addTail(NodeData nd) {
Node p = new Node(nd);
if (head == null) head = p;
else {
Node curr = head;
while (curr.next != null) curr = curr.next;
curr.next = p;
}
} //end addTail
The function
copyList
makes a copy of the list used to call it and returns the copy.
public LinkedList copyList() {
LinkedList temp = new LinkedList();
Node curr = this.head;
while (curr != null) {
temp.addTail(curr.data);
curr = curr.next;
}
return temp;
} //end copyList
The method
reverseList
reverses the order of the nodes in the given list. It works on the original list, not a copy.
public void reverseList() {
Node p1, p2, p3;
if (head == null || head.next == null) return;
p1 = head;
p2 = p1.next;
p1.next = null;
while (p2 != null) {
p3 = p2.next;
p2.next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
} //end reverseList
The function
equals
compares two linked lists. If
L1
and
L2
are two linked lists, the expression
L1.equals(L2)
is
true
if they contain identical elements in the same order and
false
otherwise.
public boolean equals(LinkedList LL) {
Node t1 = this.head;
Node t2 = LL.head;
Search WWH ::
Custom Search