You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.
Example:
Input:
5141 302 164 530 474
Output:
141 302 164 530 474
Approach:
Java
public class LinkedListAddTail {public static void main(String[] args) {SinglyLinkedList llist = new SinglyLinkedList();SinglyLinkedListNode llist_head = insertNodeAtTail(llist.head, 141);llist.head = llist_head;llist_head = insertNodeAtTail(llist.head, 302);llist.head = llist_head;llist_head = insertNodeAtTail(llist.head, 164);llist.head = llist_head;llist_head = insertNodeAtTail(llist.head, 530);llist.head = llist_head;llist_head = insertNodeAtTail(llist.head, 474);llist.head = llist_head;printLinkedList(llist.head);}static class SinglyLinkedListNode {public int data;public SinglyLinkedListNode next;public SinglyLinkedListNode(int nodeData) {this.data = nodeData;this.next = null;}}static class SinglyLinkedList {public SinglyLinkedListNode head;public SinglyLinkedListNode tail;public SinglyLinkedList() {this.head = null;this.tail = null;}}public static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {SinglyLinkedListNode temp = head;SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);newNode.next = null;if (head == null)head = newNode;else {while (temp != null && temp.next != null)temp = temp.next;temp.next = newNode;}return head;}static void printLinkedList(SinglyLinkedListNode head) {SinglyLinkedListNode temp = head;if (head == null)return;while (temp != null) {System.out.println(temp.data);temp = temp.next;}}}
C++
#include <bits/stdc++.h>using namespace std;class SinglyLinkedListNode{public:int data;SinglyLinkedListNode *next;SinglyLinkedListNode(int node_data){this->data = node_data;this->next = nullptr;}};class SinglyLinkedList{public:SinglyLinkedListNode *head;SinglyLinkedList(){this->head = nullptr;}};void print_singly_linked_list(SinglyLinkedListNode *node, string sep){while (node){cout << node->data;node = node->next;if (node){cout << sep;}}}void free_singly_linked_list(SinglyLinkedListNode *node){while (node){SinglyLinkedListNode *temp = node;node = node->next;free(temp);}}SinglyLinkedListNode *insertNodeAtTail(SinglyLinkedListNode *head, int data){SinglyLinkedListNode *temp = head;SinglyLinkedListNode *newNode = (SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode));newNode->data = data;newNode->next = NULL;if (head == NULL)head = newNode;else{while (temp != NULL && temp->next != NULL)temp = temp->next;temp->next = newNode;}return head;}int main(){SinglyLinkedList *llist = new SinglyLinkedList();int llist_count = 5;SinglyLinkedListNode *llist_head = insertNodeAtTail(llist->head, 141);llist->head = llist_head;llist_head = insertNodeAtTail(llist->head, 302);llist->head = llist_head;llist_head = insertNodeAtTail(llist->head, 164);llist->head = llist_head;llist_head = insertNodeAtTail(llist->head, 530);llist->head = llist_head;llist_head = insertNodeAtTail(llist->head, 474);llist->head = llist_head;print_singly_linked_list(llist->head, "\n");cout << "\n";free_singly_linked_list(llist->head);return 0;}
No comments:
Post a Comment