how to insert Node Q into a linked list - java

So, considering the following conditions, is this correct:
conditions:
prev references the last node of the list
curr is null
public void insertQ(SinglyLinkedListNode Q){
if (prev.next.next == curr){
prev.next = Q;
Q.next = curr;
}
return;
}
Does the previous statement insert node Q into a linked list properly?

I am not able to understand your logic in adding a node to the end of a singly linked list. Your line if (prev.next.next == curr) is undefined as prev.next is itself null. I have added a snippet for adding a node in the end of a linked list.
void append(int data){
Node node = new Node(data);
if(head==null){
head = new Node(int data);
return;
}
new_node.next = null;
Node last = head;
while (last.next != null)
last = last.next;
last.next = new_node;
return;
}

Related

Problem when alphabetically inserting a node into a doubly linked list

Im trying to write an insertion method for a doubly linked list where as i'm placing the node into the list it is being inserted in alphabetical order. This idea is I will traverse through the list with a curnode and if the newNode comes before the curnode, I will simply place the newNode before the curnode. The code I have written so far works for inserting 1 node into the list, but i'm having problems with the second part that requires checking the order and placing before. How should I change the program to make it work properly? With the code I have now only 1 element (head) will be inserted.
void insert(String x){
node curnode = head;
node newNode = new node(x);
//list is empty so insert as normal - [works fine]
if (head == null){
head = newNode;
head.prev = null;
head.next = null;
tail = newNode;
}
//Now insert node with respect to alphabetical order - [problem area]
else {
// while the list isn't empty
while (curnode != null){
// if newNode alphabetically comes before the curnode then place before
if (curnode.data.compareTo(newNode.data) > 0){
node temp = curnode.prev;
curnode.prev = newNode;
newNode.next = curnode;
newNode.prev = temp;
break;
}
}
}
}
There are a few things missing with your implementation. Compare it with this working solution:
void insert(String x){
node curnode = head;
node lastnode = null;
node newNode = new node(x);
//list is empty so insert as normal - [works fine]
if (head == null){
head = newNode;
head.prev = null;
head.next = null;
tail = newNode;
}
//Now insert node with respect to alphabetical order - [problem area]
else {
// while the list isn't empty
while (curnode != null){
// if newNode alphabetically comes before the curnode then place before
if (curnode.data.compareTo(newNode.data) > 0){
node temp = curnode.prev;
curnode.prev = newNode;
newNode.next = curnode;
newNode.prev = temp;
if(temp != null) {
temp.next = newNode;
} else {
// If newnode gets inserted in the head
head = newNode;
}
break;
}
lastnode = curnode;
curnode = curnode.next;
}
if (curnode == null) {
// insert to the last
lastnode.next = newNode;
newNode.prev = lastnode;
tail = newNode;
}
}
}

Recursive code for reversing a linked list is not working properly

Here tail is a pointer to the last element of linked list.
This code only works when there are odd numbers of nodes in a linked list and shows wrong output for even number of nodes. Please help what is the problem in the code and reason why it is happening?
public static class Node
{
int data;
Node next;
}
public static class LinkedList
{
Node head;
Node tail;
int size;
// many other member functions
private void reversePRHelper(Node node , Node prev)
{
if(node == null)
{
return;
}
Node Next = node.next;
node.next = prev;
prev = node;
reversePRHelper(Next , prev);
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
}
public void reversePR()
{
reversePRHelper(head,null);
}
}
The bug in your code is that these three lines:
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
should only be executed once, at the end, and not for each recursive call. So if you move those three statements out of your reversePrHelper method and into your reversePR method, your code will work.
private void reversePRHelper(Node node , Node prev)
{
if(node != null)
{
return;
}
Node Next = node.next;
node.next = prev;
prev = node;
reversePRHelper(Next , prev);
}
public void reversePR()
{
reversePRHelper(head,null);
Node temp = this.head;
this.head = this.tail;
this.tail = temp;
}
For me it is unclear however why you keep the tail as an attribute, since you can't navigate anywhere from the tail, as it has no next value. It would be different if your nodes would keep a reference to the previous element as well, but then it would be a double linked list.

Why the function is not working in circular Linked list?

Here is the piece of code in java for inserting node at the end of the circular linked list, but its not working using this function but when I saw in the internet and used that then it worked. Can anyone suggest me that what is wrong with this piece of code?
//PUSH NODE AT END IN CIRCULAR LL
public void pushEnd(int new_data){
Node new_node = new Node(new_data);
if(head == null){
head = new_node;
new_node.next = head;
}
else{
Node temp = head;
while(temp.next!=null){
temp = temp.next;
}
temp.next = new_node;
new_node.next = head;
}
}
Does any node in a circular linked list have its next field as null ? If it is, then its not a circular linked list. In your code you are looping until you find a null for the next. In fact after the first inserted node , for the next insert the loop while ( temp.next != null ) never ends.
You need to get to the node whose next is head, that's your last node for the moment. You need to push your new node after that.
So your code should be:
public void pushEnd(int new_data){
Node new_node = new Node(new_data);
if(head == null){
head = new_node;
new_node.next = head;
}
else{
Node temp = head;
while(temp.next!=head){
temp = temp.next;
}
temp.next = new_node //assign the temp's next to new_node.
new_node.next = head; //then assign new_node's next to head
}
}
It's a circular list. temp.next will never be null, and so your while loop will never end. You need to change your logic a bit and check when temp.next == head.

LinkedList Algorithm

Here is a implementation of LinkedList algorithm. Algorithm inserts node at beginner, after a given node, or at the end of list.
package LinkedList;
class LinkedList {
Node Head;
class Node {
int data;
Node Next;
public Node(int d) {
data = d;
Next = null;
}
}
public void insert(int value) {
if (Head ==null) {
Head = new Node(value);
return;
}
Node new_node = new Node(value);
new_node.Next = Head;
Head = new_node;
}
public void display() {
Node a = Head;
while (a != null) {
System.out.println("value:" + a.data);
a = a.Next;
}
}
public void insertMiddle(int valueToInsert, Node prev_node) {
if (Head == null) {
System.out.println("Cant put value after last node");
}
Node new_node = new Node(valueToInsert);
new_node.Next = prev_node.Next;
prev_node.Next = new_node;
}
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
}
public class LinkedList_Insertion {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList list = new LinkedList();
list.insert(8);
list.insert(20);
list.insert(0);
list.insertMiddle(999, list.Head.Next);
list.display();
System.out.println("--------------");
list.last(10000);
list.display();
}
}
In the above code, While using method insert:
public void insert(int value) {
if(Head ==null){
Head = new Node(value);
return;
}
Node new_node = new Node(value);
new_node.Next = Head;
Head = new_node;
}
why don't we use Head.next = new_node;
Similarly, for method :
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
why don't we use last.next = new_node;?
I often end up doing same mistake again and again. If someone can clear this concept, I will be grateful.
Looking forward to your response!
Head case:
You can have Head->node1->node2->node3->...->lastNode
If you do Head.next = newNode, then node1->node2->node3->...->lastNode will be lost.
If you had a doubly-linked list, you could do Head.prev = newNode; Head = Head.prev (prev means previous).
Last case:
This code:
public void last(int value){
Node new_node = new Node(value);
if(Head == null){
Head = new Node(value);
return;
}
new_node.Next = null;
Node last = Head;
while(last != null){
last = last.Next ;
}
last = new_node;
return;
}
looks weird, the condition should actually be while (last.next != null), but even then you are not inserting, you first get a reference to the element that is last in your list, then you make that reference point to another object, it should actually be last.next = newNode, you are right.
Implementing linked lists is a good way to understand how java references work, keep practicing and also try to implement a doubly-linked list.
Your Second Code seems to be wrong, You are right it has to be last.next = newNode
now answering your 1st question consider 3 nos in linked list 100(head),200,300.
here head node with value 100 points to next node with value 200 which must in turn point to the node with value 300. Now let us suppose you want to insert 50 before 100, so when you do
Head.next = new_node;
The node with value 100 is made to point to next node with value 50, so now we have 100,50 in the linked list with the head as 100 still which is wrong
so we do
new_node.Next = Head;
Head = new_node;
In this case linked list becomes 50,100,200,300.
Thus we do it this way.

Reversing a linked list

My problem is: Given a function to reverse a linked list.
My attempt at it in C was:
ListNode *reverse(ListNode *head)
{
if(head == NULL || head->next == NULL)
return head;
ListNode *temp = head->next;
ListNode *retP = reverse(temp);
temp->next = head;
head->next = NULL;
return retP;
}
But I do not think this is right. I want to be able to do it in Java and I am stumped on this. Any help would be appreciated. Please help me get started
If you want to reverse a List in Java, use
Collections.reverse(List list)
If you want to know how it is implemented or want to do it by hand, have a look at the JDK sources of java.util.Collections.
Iteratively
public reverseListIteratively (Node head) {
if (head == NULL || head.next == NULL)
return; //empty or just one node in list
Node Second = head.next;
//store third node before we change
Node Third = Second.next;
//Second's next pointer
Second.next = head; //second now points to head
head.next = NULL; //change head pointer to NULL
//only two nodes, which we already reversed
if (Third == NULL)
return;
Node CurrentNode = Third;
Node PreviousNode = Second;
while (CurrentNode != NULL)
{
Node NextNode = CurrentNode.next;
CurrentNode.next = PreviousNode;
/* repeat the process, but have to reset
the PreviousNode and CurrentNode
*/
PreviousNode = CurrentNode;
CurrentNode = NextNode;
}
head = PreviousNode; //reset the head node
}
Recursively
public void recursiveReverse(Node currentNode )
{
//check for empty list
if(currentNode == NULL)
return;
/* if we are at the TAIL node:
recursive base case:
*/
if(currentNode.next == NULL)
{
//set HEAD to current TAIL since we are reversing list
head = currentNode;
return; //since this is the base case
}
recursiveReverse(currentNode.next);
currentNode.next.next = currentNode;
currentNode.next = null; //set "old" next pointer to NULL
}
Source, with explanation (after using google for 3 seconds) http://www.programmerinterview.com/index.php/data-structures/reverse-a-linked-list/
public Node reverse(Node node){
Node p=null, c=node, n=node;
while(c!=null){
n=c.next;
c.next=p;
p=c;
c=n;
}
return p;
}

Categories

Resources