Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to the head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty.
Example:
Input:
5383484392975321
Output:
321 975 392 484 383
Approach
Java
public class LinkedListAddHead {public static void main(String[] args) {SinglyLinkedList llist = new SinglyLinkedList();SinglyLinkedListNode llist_head = insertNodeAtHead(llist.head, 383);llist.head = llist_head;llist_head = insertNodeAtHead(llist.head, 484);llist.head = llist_head;llist_head = insertNodeAtHead(llist.head, 392);llist.head = llist_head;llist_head = insertNodeAtHead(llist.head, 975);llist.head = llist_head;llist_head = insertNodeAtHead(llist.head, 321);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 insertNodeAtHead(SinglyLinkedListNode head, int data) {SinglyLinkedListNode temp = head;SinglyLinkedListNode newNode = new SinglyLinkedListNode(data);if (head == null) {head = newNode;head.next = null;} else {temp = head;newNode.next = temp;head = 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;SinglyLinkedListNode *tail;SinglyLinkedList(){this->head = nullptr;this->tail = 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 *insertNodeAtHead(SinglyLinkedListNode *head, int data){SinglyLinkedListNode *newNode = (SinglyLinkedListNode *)malloc(sizeof(SinglyLinkedListNode));newNode->data = data;if (head == NULL){head = newNode;head->next=NULL;}else{SinglyLinkedListNode *temp = head;newNode->next = temp;head = newNode;}return head;}int main(){SinglyLinkedList *llist = new SinglyLinkedList();int llist_count = 5;SinglyLinkedListNode *llist_head = insertNodeAtHead(llist->head, 383);llist->head = llist_head;llist_head = insertNodeAtHead(llist->head, 484);llist->head = llist_head;llist_head = insertNodeAtHead(llist->head, 392);llist->head = llist_head;llist_head = insertNodeAtHead(llist->head, 975);llist->head = llist_head;llist_head = insertNodeAtHead(llist->head, 321);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