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;
}
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;
}
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;
}
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;
}