We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In Java there is no concepts of pointers exist...
1. {so we will be using a node tempraraily which will have both next(refrence field) and value (data field)}. Unlike pointers in C which only contain references(address).
2.temp is a temprary node created which is used to traverse the singly link list.. (temp=head) means i am assigning the (same refrence and off course same value) to the temp node to which head is pointing.
3.If temp is null means head will also be null which means no node exist(linked list is empty) so we simply create a node and named it head and we returned head.
4.And if our Linked list if not empty(head!=null) then temp node is travesed till the end of LL untill when (temp.next==null)(until now temp is last node)..we will create a new node containing data(value) which is passed as parameter..and its next will be null..(now temp is second last node and it is holding refrence of newly created node).
5.Another approach is by using the tail refrence..but in our question we are provided with Head(at line no 73.)
Insert a Node at the Tail of a Linked List
You are viewing a single comment's thread. Return to all comments →
In Java there is no concepts of pointers exist... 1. {so we will be using a node tempraraily which will have both next(refrence field) and value (data field)}. Unlike pointers in C which only contain references(address). 2.temp is a temprary node created which is used to traverse the singly link list.. (temp=head) means i am assigning the (same refrence and off course same value) to the temp node to which head is pointing. 3.If temp is null means head will also be null which means no node exist(linked list is empty) so we simply create a node and named it head and we returned head. 4.And if our Linked list if not empty(head!=null) then temp node is travesed till the end of LL untill when (temp.next==null)(until now temp is last node)..we will create a new node containing data(value) which is passed as parameter..and its next will be null..(now temp is second last node and it is holding refrence of newly created node). 5.Another approach is by using the tail refrence..but in our question we are provided with Head(at line no 73.)