Problem: Reverse a singly linked list recursively.
I know how to solve this problem, but one of my recursive methods is wrong, I can not figure out what's wrong with this code. Could anyone figure out that? Thanks a lot!
Test case:
Input:
[1,2,3]
Output:
[3,1]
Expected:
[3,2,1]
public class Solution {
// recursive
ListNode last = null;
public ListNode reverseList(ListNode head) {
if (head == null) return null;
helper(head);
return last;
}
private ListNode helper(ListNode head) {
// base case
if (head.next == null) {
last = head;
return head;
}
// general case
ListNode prev = reverseList(head.next); // should be ListNode prev = helper(head.next);
prev.next = head;
head.next = null;
return head;
}
}
It doesn't work, because in the first pass of the helper method
ListNode prev = reverseList(head.next);
returns [3, 2], and then you assign your old head (1) to be the next node after the new head of the reversed list (3). Therefore, the end result is [3, 1].
Related
I am trying to solve leetcode-148 (https://leetcode.com/problems/sort-list/) i.e. sort given LinkedList, but am getting a stackoverflow error. so far I have tried dry running but am not seeing where the issue could occur.. the base condition of the recursion seems to be right but looks like I am missing something if someone sees what I am not seeing..
/**
* 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 sortList(ListNode head) {
if (head==null || head.next==null) return head;
ListNode follow = new ListNode(0);
follow.next=head;
ListNode fast = head;
ListNode slow = head;
// Find the mid-point of the list
while (fast.next!=null && fast.next.next!=null) {
slow=slow.next;
fast=fast.next.next;
follow=follow.next;
}
// Split the list
follow.next = null;
// Sort each half
ListNode first = sortList(head);
ListNode second = sortList(slow);
// Merge
return merge(first, second);
}
private ListNode merge(ListNode first, ListNode second) {
if (first==null) return second;
if (second==null) return first;
ListNode result = new ListNode(0);
ListNode head = result;
while (first!=null && second!=null) {
if (first.val<second.val) {
result.next = first;
} else {
result.next = second;
}
result=result.next;
}
if (first!=null) {
result.next = first;
result=result.next;
}
if (second!=null) {
result.next = second;
result=result.next;
}
return head.next;
}
}
Here's the error
WARNING: A command line option has enabled the Security Manager
WARNING: The Security Manager is deprecated and will be removed in a future release
java.lang.StackOverflowError
at line 31, Solution.sortList
at line 31, Solution.sortList
at line 31, Solution.sortList
at line 31, Solution.sortList
at line 31, Solution.sortList
There are two issues:
When sortList is called with a list that has 2 nodes, then after the first loop (which makes no iterations), slow will be equal to head, and follow.next = null will just mutate the dummy node that was prepended before the head node. So essentially the linked list wasn't split, and the recursive call is on the same list, leading to infinite recursion. Solve this by changing the while condition so at least one iteration will be made. Also, you can do this without a third reference (follow).
In merge, the first and second references do not move forward, so the loop will be infinite.
Here is corrected code:
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode fast = head.next;
ListNode slow = head;
// Find the mid-point of the list
while (fast != null && fast.next != null) { // iterate at least once
slow = slow.next;
fast = fast.next.next;
}
// Split the list
ListNode second = slow.next;
slow.next = null;
// Sort each half
head = sortList(head);
second = sortList(second);
// Merge
return merge(head, second);
}
private ListNode merge(ListNode first, ListNode second) {
ListNode result = new ListNode(0);
ListNode head = result;
while (first != null && second != null) {
if (first.val < second.val) {
result.next = first;
first = first.next; // move forward
} else {
result.next = second;
second = second.next;
}
result = result.next;
}
if (first != null) {
result.next = first;
result = result.next;
}
if (second != null) {
result.next = second;
result = result.next;
}
return head.next;
}
I am new to recursion concept and while practicing I came across a problem for which I am not able to get a logical reasoning.
For the below code snippet, first element of the link is not getting printed (Assume list has more than one elements).
public void foo(ListNode head) {
foo1(head, head.next);
}
private void foo1(ListNode curr, ListNode nextNode) {
if (nextNode == null) {
return;
}
curr = nextNode;
nextNode = curr.next;
foo1(curr, nextNode);
System.out.println(curr);
}
Now for example if list has 3 elements as 1 -> 2 -> 3 -> null, only 3 and 2 are getting printed. foo method made a call with head element which is one so shouldn't it print 1 aslo in the output.
Please help me understand what I am doing wrong here.
curr = nextNode;
...
System.out.println(curr);
The problem is you set the curr to its successor before you print it.
private void foo1(ListNode curr) {
if (curr == null)
return;
System.out.println(curr);
foo1(curr.next);
}
Reason
The reason is, before printing the first element, your code changes the value from the first element to the second element. One more issue also present in your code, when we pass one element, it won't print that one element also. Because consider this you have a node 5 and the next node is null. When you pass this node to foo1() method, immediately it reaches the condition nextNode is null and it returns, here there is no chance to print the first node 5.
I modified your same code to run perfectly as expected, look below.
public void foo(ListNode head) {
foo1(head, head.next);
}
private void foo1(ListNode curr, ListNode nextNode) {
if(curr != null)
System.out.println(curr);
if (nextNode == null) {
return;
}
curr = nextNode;
nextNode = curr.next;
foo1(curr, nextNode);
}
Below is my code. I'm trying to return head node back after I insert value to either left or right node. I understood the concept of insertion, but I'm unable to understand how can I return my head node back to that now it is back to original state with addition node added.
Here is exactly I don't understand.
When I insert my node how can I break the loop and return its head node back.
Recursion is stack concept which will output based on LIFO and if it is lifo how can I have head node returned back
Here's my code:
class Node {
int data;
Node left;
Node right;
}
static Node Insert(Node root,int value)
{
return nodeHelper(root,value);
}
static Node nodeHelper(Node root,int value){
Node nodeTracker = root;
Node temp;
if(root!=null){
if(value>root.data){
if(root.right==null){
temp =new Node();
temp.data = value;
root.right = temp;
return nodeTracker;
}
else{
nodeHelper(root.right,value);
}
}
else{
if(root.left==null){
temp=new Node();
temp.data = value;
root.left = temp;
return nodeTracker;
}
else{
nodeHelper(root.left,value);
}
}
}
else{
temp=new Node();
temp.data = value;
return temp;
}
}
}
To return the root of the tree, you need a third parameter that you pass around to keep track of the root. Like this:
Node* nodeHelper(Node* nodeTracker, Node* parent, int value)
Remove the local nodeTracker variable.
Your recursive calls become:
return nodeHelper(nodeTracker, parent.left, value);
(and, of course, same thing for the right branch)
And your initial call in the insert function is:
return nodeHelper(root, root, value);
Here is what I've done so far:
struct rep_list {
struct node *head;
struct node *tail;
}
typedef rep_list *list;
int length(const list lst) {
if (lst->head == NULL) {
return 0;
}
else {
lst->head = lst->head->next;
return 1 + length(lst);
}
}
This works, but the head of the list the function accepts as a parameter gets changed. I don't know how to fix that.
I'm not allowed to change the function definition so it should always accept a list variable.
Any ideas?
EDIT: I tried to do what Tyler S suggested in the comments but I encountered another problem. If I create a node* variable at the beginning, it should point to lst->head. But then every recursive call to the function changes the value back to lst->head and I cannot move forward.
You don't need a local node: just don't change the list head. Instead, pass the next pointer as the recursion head.
int length(const list lst) {
if (lst->head == NULL) {
return 0;
}
else {
return 1 + length(lst->head-next);
}
}
I see. Okay; this gets a bit clunky because of the chosen representation. You need a temporary variable to contain the remaining list. This iscludes changing the head.
int length(const list lst) {
if (lst->head == NULL) {
return 0;
}
else {
new_lst = new(list)
new_lst->head = lst->head->next;
var result = 1 + length(new_lst);
free(new_lst)
return result
}
}
At each recursion step, you create a new list object, point it to the 2nd element of the current list, and continue. Does this do the job for you?
Although this solution is clunky and I hate it, its the only way I can see to accomplish what you're asking without modifying the method signature. We create a temporary node * as member data of the class and modify it when we start.
struct rep_list {
struct node *head;
struct node *tail;
}
node *temp = NULL;
bool didSetHead = false;
typedef rep_list *list;
int length(const list lst) {
if ((didSetHead) && (lst->head != temp)) {
temp = lst->head;
didSetHead = false;
}
if (temp == NULL) {
didSetHead = true;
return 0;
}
else {
temp = temp->next;
return 1 + length(temp);
}
}
Please note, I haven't tested this code and you may have to play with a bit, but the idea will work.
Following is the code that I found online to find the minimum depth of a binary search tree:
public class Solution {
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();
LinkedList<Integer> counts = new LinkedList<Integer>();
nodes.add(root);
counts.add(1);
while(!nodes.isEmpty()){
TreeNode curr = nodes.remove();
int count = counts.remove();
if(curr.left != null){
nodes.add(curr.left);
counts.add(count+1);
}
if(curr.right != null){
nodes.add(curr.right);
counts.add(count+1);
}
if(curr.left == null && curr.right == null){
return count;
}
}
return 0;
}
}
What I do not understand is the extra return statement at the end- return 0. Why is this needed?
It's for the case where the root isn't null, but it's the only node in the tree (the root is at depth 0). That return statement is needed because if the tree is empty, then something must be returned. It returns 0, because the depth is 0.
Similar to ghostofrasputin, the return statement is there because if the while condition is not met, then there is still a value to return.
Now the more important question, why do we need the last return if the program will never reach that return statement? (which I believe is this case for this)
Even though you are able to tell that the return statement wont be used, the compiler is not sophisticated enough to determine that, so it requires a return statement just in case the while loop is exited.
It's similar to the following code
public boolean getTrueOrFalse() {
if(Math.random() < 1) {
return true;
}
return false;
}
Though we know that this will always return true because Math.random() is always less than 1, the compiler isn't able to figure that out and thus the return statement is required if the if-statement is not met.