Search code examples
javadata-structureslinked-listnodessingly-linked-list

How to print the linkedlist from the middle node to the last node?


I'm trying to solve the LeetCode problem 876. Middle of the Linked List:

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.

I'm trying to run my code on VS Code and trying to take the input list from the user and printing the linked list from the middle node till the last node.

I'm not able to print the desired linked list, instead my code prints the last element of the list. I've seen other answers related to linked list and all but still don't know what I'm doing wrong. Can you help me in solving this?

Here's 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; }
}

Class to find the middle of the list and return the list node from the middle:

public class Solution {
    public ListNode middleNode(ListNode head) {
         ListNode slow=head, fast=head;
        while( fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;    
        }       
        return slow;
    }
}

Main class:

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        ListNode ll= new ListNode();
        System.out.print("Enter the size of linked list: ");
        int size = sc.nextInt();
        System.out.println("Enter the elements of linked list: ");
        for(int i =1 ; i<=size ; i++){
            ll = new ListNode(sc.nextInt());
        }
        sc.close();
        Solution ob = new Solution();
        ListNode res=ob.middleNode(ll);
        System.out.println("Linked list starting from the: ");    
        while(res != null) {       
            System.out.print(res.val + " ");    
            res = res.next;    
        }    
        System.out.println(); 
    }
}

Solution

  • import java.util.Scanner;
    public class Main {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            ListNode ll= new ListNode();
            System.out.print("Enter the size of linked list: ");
            int size = sc.nextInt();
             System.out.println("Enter the elements of linked list: ");
             for(int i =1 ; i<=size ; i++){
                 // every time the new called, ll will be reseted.
                 ll = new ListNode(sc.nextInt());
             }
            sc.close();
            Solution ob = new Solution();
            ListNode res=ob.middleNode(ll);
            System.out.println("Linked list starting from the: ");    
            while(res != null) {       
                System.out.print(res.val + " ");    
                res = res.next;    
            }    
            System.out.println(); 
        }
    }
    

    the problem is that when calling the new operation, ll will be resetted, and you only get the last node, see the comment in prevous code, the following code show one of the correct example.

    
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            ListNode ll = null, head = null;
            System.out.print("Enter the size of linked list: ");
            int size = sc.nextInt();
             System.out.println("Enter the elements of linked list: ");
             for(int i =1 ; i<=size ; i++){
                 if (ll == null) {
                   ll = new ListNode(sc.nextInt());
                   head = ll;
                 } else {
                   ll.next = new ListNode(sc.nextInt())
                   ll = ll.next;
                }
             }
            sc.close();
            Solution ob = new Solution();
            ListNode res=ob.middleNode(head);
            System.out.println("Linked list starting from the: ");    
            while(res != null) {       
                System.out.print(res.val + " ");    
                res = res.next;    
            }    
            System.out.println(); 
        }
    }