0

In this linked list I am trying to delete a node and return the list after deleting the particular node. Here I am not using the Value to delete, I am using the position to delete that particular node. Here in my code the delete function seems to have no effect over the output. What am I doing wrong here?

import java.util.*; class LinkedList{ Node head; static class Node{ int data; Node next; Node(int d){ this.data = d; next = null; } } public LinkedList insert(LinkedList l, int data){ Node new_node = new Node(data); if(l.head == null){ l.head = new_node; } else{ Node last = l.head; while(last.next != null){ last = last.next; } last.next = new_node; } return l; } public LinkedList delete(LinkedList l, int position){ Node current = l.head; if(position == 0){ current = current.next; } int index = 1; while(index < position - 1){ current = current.next; index++; } current = current.next.next; Node iterating = l.head; while(iterating != null){ System.out.print(iterating.data + " "); iterating = iterating.next; } return l; } } public class Main { public static void main(String[] args) { LinkedList l = new LinkedList(); Scanner sc = new Scanner(System.in); int number = sc.nextInt(); int position = sc.nextInt(); for(int i=0; i<number; i++){ int num = sc.nextInt(); l.insert(l,num); } l.delete(l,position); } } 
3
  • I've just copy-pasted your code to my IDE and it is showing that current = current.next.next; in your delete method is not then used. Commented Sep 6, 2021 at 16:41
  • 1
    Why are you passing a list into these functions? I'd expect to be able to call thise by writing "someList.delete(someIndex)". In any case, the code never changes anything in the list, just the local node reference. Commented Sep 6, 2021 at 16:43
  • Hi @sailesh, can I kindly remind you to mark one of the solutions below as answer so that system knows this question was answered. Commented Sep 16, 2021 at 8:28

2 Answers 2

1

current=current.next doesn't have any effect on the original LinkedList because with that line of code, you just change where the reference (named as current) points to. Think about this way,

Node a = new Node() Node b = new Node() Node c = new Node() a.next =b a = c 

These lines of code doesn't result in c being connected to b.

Change code to this:

if (position == 0) return l.head.next; else { Node head = l.head; int index = 1; Node itr = l.head; while(index < position) { itr = itr.next; index++; } if(itr.next != null) itr.next = itr.next.next; return head; } 
Sign up to request clarification or add additional context in comments.

Comments

0
public LinkedList delete(LinkedList l, int position) { Node previous = null; Node current = l.head; int index = 0; while (current != null && index < position){ previous = current; current = current.next; index++; } if (current != null) { if (previous == null) { l.head = current.next; } else { previous.next = current.next; } } System.out.print("["); Node iterating = l.head; while (iterating != null) { System.out.print(iterating.data + ", "); iterating = iterating.next; } System.out.println("]"); return l; } 

The problem in java is that to delete a node either the head must be changed to its next, or the previous node's next must be changed to current's next.

Then too current might become null, have reached the list's end, position > list length.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.