Search in sources :

Example 26 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class MayW2 method mergeSort.

public ListNode mergeSort(ListNode head) {
    ListNode fast = head;
    ListNode middle = head;
    ListNode prev = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        prev = middle;
        middle = middle.next;
    }
    if (head == middle)
        return head;
    // split into 2 halves
    prev.next = null;
    ListNode first = mergeSort(head);
    ListNode second = mergeSort(middle);
    ListNode sorted = mergeSortedListIterative(first, second);
    return sorted;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 27 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class MayW3 method oddEvenList.

/* https://leetcode.com/problems/odd-even-linked-list/ */
public ListNode oddEvenList(ListNode head) {
    ListNode odd = head;
    if (head == null || head.next == null) {
        return head;
    }
    ListNode even = odd.next;
    ListNode evenStart = even;
    while (even != null && even.next != null) {
        ListNode newEven = even.next.next;
        ListNode newOdd = even.next;
        odd.next = newOdd;
        newOdd.next = evenStart;
        odd = newOdd;
        even.next = newEven;
        even = newEven;
    }
    return head;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 28 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class AugustW4 method removeZeroSumSublists.

public ListNode removeZeroSumSublists(ListNode head) {
    res = new ListNode(0);
    removeSubElements(head);
    return res.next;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Example 29 with ListNode

use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.

the class A method isPalindrome.

/* 234. Palindrome Linked List
    * https://leetcode.com/problems/palindrome-linked-list/
    * */
public boolean isPalindrome(ListNode head) {
    ListNode fastPtr = head;
    ListNode slowPtr = head;
    while (fastPtr != null && fastPtr.next != null) {
        fastPtr = fastPtr.next.next;
        slowPtr = slowPtr.next;
    }
    ListNode newHead = null;
    while (slowPtr != null) {
        ListNode next = slowPtr.next;
        slowPtr.next = newHead;
        newHead = slowPtr;
        slowPtr = next;
    }
    while (head != null && newHead != null) {
        if (head.val != newHead.val) {
            return false;
        }
        head = head.next;
        newHead = newHead.next;
    }
    return true;
}
Also used : ListNode(com.pkumar7.datastructures.ListNode)

Aggregations

ListNode (com.pkumar7.datastructures.ListNode)29 TreeNode (com.pkumar7.TreeNode)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Stack (java.util.Stack)1