Linked List. Insert integers in order - java

I have a linked list of integers. When I insert a new Node I need to insert it not at the end, but in oder... i.e. 2, 4, 5, 8, 11, 12, 33, 55, 58, 102, etc. I don't think I am inserting it in the correct position. Do see what Im doing wrong?
Node newNode = new Node(someInt);
Node current = head;
for(int i=0; i<count; i++){
if(current == tail && tail.data < someInt){
tail.next = newNode;
}
if(current.data < someInt && current.next.data >= someInt){
newNode.next = current.next;
current.next = newNode;
}
}

I think this might be closer to what you are looking for.
Node newNode = new Node(someInt);
Node current = head;
//check head first
if (current.data > newNode.data) {
newNode.next = head;
head = newNode;
}
//check body
else {
while(true){
if(current == tail){
current.next = newNode;
tail = newNode;
break;
}
if(current.data < someInt && current.next.data >= someInt){
newNode.next = current.next;
current.next = newNode;
break;
}
current = current.next;
}
}

You're never moving forward in the list. you need an else that sets:
current = current.next
You can also add a break statement after you've inserted the node since at that point you're done with the loop.

It doesn't look like you're updating current... try inserting something like this in your loop:
current = current.next;

Looks like you missing the case, when the new element is lesser than all existing ones.

Related

Can't add node at index is list is empty in a doubly linked list

I have created a doubly-linked list that I fill with nodes in Java. I am trying to make a method that adds a node at a certain index. The problem is that I get NullPointerException if I try to add a node and there are no other nodes. So I would write list.add(0, 0); for example and I would get NullPointerException (the first number is index and second is value). However, if I first use my method that just puts in a node and then tries list.add(0, 0); or list.add(1, 0); it works. I believe the problem lies in how to iterate through the nodes. The code currently looks like this:
void add(int index, T t)
{
ListNode<T> node = header;
ListNode<T> newNode = new ListNode<T>(t);
if(index < 0 || index > size)
{
throw new IndexOutOfBoundsException("Index not allowed");
}
int counter = 0;
while(node.next != null && counter < index)
{
node = node.next;
counter++;
}
if(counter == 0)
{
header = newNode;
newNode.next = node;
}
else
{
node = node.previous;
ListNode<T> temp = node.next;
node.next = newNode;
newNode.next = temp;
}
newNode.previous = node;
newNode.next.previous = newNode;
size++;
}
And I believe that this is where the problem is:
while(node.next != null && counter < index)
Do you have an idea on how to do it instead? I can't think of another way.

Replacing Node in LinkedList not actually replacing anything

I am currently writing an implementation of a doubly linked list and am having trouble setting a node to a certain value. Since the data in the nodes is final, I need to replace the entire node, but the code that I wrote for that seems to be working until it exits the method and the original list hasn't been changed at all. Is there anything that I'm missing in the code here?
`public void set(int index, String item) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
}
Node curr = this.front;
for (int i = 0; i < index; i++) {
curr = curr.next;
}
Node temp = new Node(item);
temp.prev = curr.prev;
temp.next = curr.next;
curr = temp;
}`
You should also change "next of the previous node" and "prev of the next node". Assume prev is the previous node of cur and next is the next node of cur.
Node temp = new Node(item);
temp.prev = prev;
temp.next = next;
next.prev = temp;
prev.next = temp;
You should deal with edge problems.
you can
System.out.println(curr.val);
and you find the print is actually the same as item
but when you return ,you will find nothing change
you just need find the pre node of curr and set pre.next= curr(where is changed)
`public void set(int index, String item) {
if (index < 0 || index >= this.size) {
throw new IndexOutOfBoundsException();
}
Node curr = this.front;
for (int i = 0; i < index; i++) {
curr = curr.next;
}
Node temp = new Node(item);
temp.prev = curr.prev;
temp.next = curr.next;
curr = temp;
//here you need to set maybe
// Node tmp=curr.prev;
// tmp.next=curr;
}`

Inserting a Node after a given position in a singly linked list

My code works fine in Eclipse but not in a compiler in a programming site. This code is about adding a node at given position.
Node InsertNth(Node head, int data, int position) {
Node n = new Node();
Node last = head;
int count = 0;
if (position == 0) {
n.data = data;
n.next = head;
head=n;
return n;
}
while (count < position) {
count++;
last = last.next;
}
n.data = data;
n.next = last.next;
last.next = n;
return head;
}
You go too far with the loop, and also don't check the position is in range:
Node InsertNth(Node head, int data, int position) {
Node n = new Node();
n.data = data;
if (position == 0) {
n.next = head;
return n;
}
Node last = head;
for (int count = 0; count < position-1; count++) {
if (last == null) {
return null;
}
last = last.next;
}
n.next = last.next;
last.next = n;
return head;
}
Also, a for loop is more suitable, and some other things could be rearranged better.

Deleting a Node after a given position and head in a singly linked list

Did I miss anything in the below code . The code is about deleting a Node from a linkedlist given its head and position. My program is not passing all the test cases .
Node Delete(Node head, int position) {
// Node temp=head;
int count=1;
if(position==0){
head=head.next;
return head;
}
if(position==1){
head=head.next.next;
return head;
}
if(position>1){
Node temp=head;
while(count<position){
count++;
temp=temp.next;
}
temp.next=temp.next.next;
}
return head;
}
Input
4
3
1 2 3
0
3
1 2 3
1
3
1 2 3
2
5
4 3 2 5 1
2
My output
23
12
4351
Expected output
23
13
12
4351
public static Node Delete(Node head, int position) {
Node node = head;
Node prevNode = null;
int index = 0;
if (head == null && position == 0){
return head;
}
if (head != null && position == 0){
head = null;
head = node.next;
}
if (position > 0){
while(index<position){
prevNode = node;
node = node.next;
index = index + 1;
}
prevNode.next = node.next;
node = null;
}
return head;
}

does this merge sort use O(1) extra space?

Just for my clarification, constant space means O(1) space, right?
Anyway, here is a code i have written, please tell me if it uses O(1) space.
void mergeSort() {
ListNode merge = this.head;
this.head = mergeSort(merge);
}
ListNode mergeSort(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode slow = head; ListNode fast = head.next;
while(fast!=null && fast.next !=null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode left = head; ListNode right = slow.next;
slow.next = null;
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
ListNode merge(ListNode L, ListNode R) {
if(L==null) return R;
if(R==null) return L;
ListNode h = null;
if(L.val <= R.val )){
h = L;
h.next = merge(L.next, R);
} else {
h = R;
h.next = merge(L, R.next);
}
return h;
}

Categories

Resources