Type Here to Get Search Results !

Palindrome linked list leetcode 100% solution.

0

 Palindrome linked list leetcode 100% solution.


Palindrome linkled list java solution.

Given the head of a singly linked list, return true if it is a palindrome.

Latest Engineering IT Jobs Telegram Channel - Click Here

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null && list2 == null) {
            return null;
        }
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        ListNode head = null;
        ListNode result = null;
        if (list1.val < list2.val) {
            result = list1;
            head = result;
            list1 = list1.next;
        } else {
            result = list2;
            head = result;
            list2 = list2.next;
        }
        while (list1 != null || list2 != null) {
            
            
            if ((list1 != null) && (list2 != null)) {
                if (list1.val <= list2.val) {
                    result.next = list1;
                    list1 = list1.next;
                } else {
                    result.next = list2;
                    list2 = list2.next;
                }
            } else if (list2 == null) {
                result.next = list1;
                list1 = list1.next;
            } else if (list1 == null) {
                result.next = list2;
                list2 = list2.next;
            } else {
                break;
            }
            result = result.next;
        }    
        return head;
    }
}
Run Code :- Click Here


Post a Comment

0 Comments