how does linked list add to tail code example
Example 1: adding an element to the end of a linked list java
class Node {
Object data;
Node next;
Node(Object d,Node n) {
data = d ;
next = n ;
}
public static Node addLast(Node header, Object x) {
Node ret = header;
if (header == null) {
return new Node(x, null);
}
while ((header.next != null)) {
header = header.next;
}
header.next = new Node(x, null);
return ret;
}
}
Example 2: how to add to the end of a linked list
typedef struct node{
int value;
struct node *next;
}node;
node *append(node *head, int val){
node *tmp = head;
node *createdNode = createNode(val);
while(tmp->next != NULL){
tmp = tmp->next;
}
tmp->next = createdNode;
return head;
}