203. Remove Linked List Elements from the leetcode - java

This code has to remove the node of the value sent from LinkedList where head and the value is given
Can someone tell me what is wrong with the code?
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null) return head;
if(head.val==val) {head=head.next;}
ListNode currentNode = head;
ListNode previousNode = head;
while(currentNode.val!=val) {
previousNode=currentNode;
currentNode = currentNode.next;
}
previousNode.next=currentNode.next;
removeElements(currentNode.next,val);
return head;
}
}
For the test case [7,7,7,7] expected answer is [] but the code is giving the output [7,7,7]

you just need to pass through the list and checking the current node and update the reference if needed
public ListNode removeElements(ListNode head, int val) {
if(head==null) return null;
if(head.val==val) return removeElements(head.next,val);
head.next = removeElements(head.next,val);
return head;
}

Related

java infinite linked list issue

So I'm working this problem:Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.And test example is [1,1,2].
public static class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public static ListNode deleteDuplicates(ListNode head) {
ListNode l3=new ListNode(9);
ListNode l4 = l3;
if (head==null||head.next==null){
return head;
}
while (head.next!=null){
if(head.val== head.next.val){
head=head.next;
l3.next=head;
}else {
l3.next=head;
head=head.next;
//when I debug until this line,it will creat infinite list like[1,1,1,1,.......,1,1,1]
}
l3=l3.next;
}
return l4.next;
}
So as you can see in the code,why will it creat infinite list when test example is[1,1,2]?

Instantiating Java Class and then adding values to it

So through LeetCode, I picked a question that asks to add 2 numbers from singly linked list. The following is the structure of LinkedList
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode newNode = new ListNode(0);
ListNode head = newNode;
while(l1 != null)
{
newNode((((l1.val)+(l2.val))%10));
l1 = l1.next;
l2 = l2.next;
newNode = newNode.next;
newNode = new ListNode();
}
newNode.next = null;
return head;
}
}
It gives an error saying "cannot find symbol newNode....."
Assuming both lists are the same size or length, I first instantiated ListNode class to newNode, and then within while loop, I am simply calling the same class since it has a constructor that takes 1 parameter and I pass the value from both list module 10.
Its been a while since I am doing Java, is this not allowed in Java? I know the solution but I just don't know why the above won't work?

How to take a list as input using the defined ListNode class

I was trying to construct a linked list using the above listNode class can someone help me how I can take inputs and construct the linked List in Java
Below is the code:
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
The code you are trying to use does not contain necessary functions for the linked list.
Its only a node in a linked list.
Below is an implementation of Linked List in Java you can use:
class Node {
public int data;
public Node next;
public void displayNodeData() {
System.out.println("{ " + data + " } ");
}
}
public class SinglyLinkedList {
private Node head;
public boolean isEmpty() {
return (head == null);
}
// used to insert a node at the start of linked list
public void insertFirst(int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = head;
head = newNode;
}
// used to delete node from start of linked list
public Node deleteFirst() {
Node temp = head;
head = head.next;
return temp;
}
// Use to delete node after particular node
public void deleteAfter(Node after) {
Node temp = head;
while (temp.next != null && temp.data != after.data) {
temp = temp.next;
}
if (temp.next != null)
temp.next = temp.next.next;
}
// used to insert a node at the start of linked list
public void insertLast(int data) {
Node current = head;
while (current.next != null) {
current = current.next; // we'll loop until current.next is null
}
Node newNode = new Node();
newNode.data = data;
current.next = newNode;
}
// For printing Linked List
public void printLinkedList() {
System.out.println("Printing LinkedList (head --> last) ");
Node current = head;
while (current != null) {
current.displayNodeData();
current = current.next;
}
System.out.println();
}
}
However, i would prefer you use
this when you are using linked list in an application.

java: Memory Limit Exceeded?

I write a code by java in leetCode, this is the link:
https://leetcode.com/problems/reverse-linked-list/description/
it shows "Memory Limit Exceeded", can anyone explain why?(you can just paste my code to the above link to see the error)
My code is as follows:
public static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode reverseList(ListNode head) {
if(head ==null)
return head;
if(head.next ==null){
return head;
}
Stack<ListNode> mStack =new Stack<>();
while(head!=null){
mStack.push(head);
head = head.next;
}
ListNode mNode = new ListNode(0);
ListNode result =mNode;
while(!mStack.empty()){
ListNode temp = mStack.pop();;
mNode.next = temp;
mNode = mNode.next;
}
return result.next;
}
The problem is that, suppose the input is 1->2->3. Then what you will return is
3->2->1->2->1->2.....
This circular linked list will cause Memory Limit Exceeded when calling toString method.
To solve this, just set the next of original head to null.
This is because they expect you to do it in constant space complexity. A simple recursive solution would be :
class Solution {
public ListNode reverseList(ListNode head) {
if (head==null){
return head;
}
return reverseList(head,null);
}
public ListNode reverseList(ListNode current,ListNode prev){
if (current.next==null){
// we have reached the last node. This will be the new head
current.next = prev;
return current;
}
ListNode head = reverseList(current.next,current);
current.next=prev;
return head;
}
}

What's wrong with my recursion? Trying to add two node in each traversal

Seems like my linear linked list didn't connect for each recursion. Need explanation why my code doesn't work.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null)
return null;
int val = l1.val + l2.val;
ListNode newNode = new ListNode(val);
System.out.println(newNode.val);
return newNode.next = addTwoNumbers(l1.next, l2.next);
}
}
Your input
[2,4,3]
[5,6,4]
Your stdout
7
10
7
Your answer
[]
Expected answer
[7,10,7]
I think that you didn't return newNode.next.
You must call recursion method with newNode.next but you must return newNode.
Ps. Remember to add if (l2==null) return null;
Change
return newNode.next = addTwoNumbers(l1.next, l2.next);
To
newNode.next = addTwoNumbers(l1.next, l2.next);
return newNode

Categories

Resources