use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class MayW2 method main.
// TODO: 5/15/2020
/* https://leetcode.com/problems/minimum-cost-for-tickets/ */
/* https://leetcode.com/problems/all-oone-data-structure/ */
/* https://leetcode.com/problems/couples-holding-hands/ */
/* https://leetcode.com/problems/increasing-subsequences/ */
/* https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/ */
/* https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/*/
/* DP on trees */
/*
https://leetcode.com/problems/sum-of-distances-in-tree/
https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/
https://leetcode.com/problems/unique-binary-search-trees-ii/
*/
public static void main(String[] args) {
MayW2 w2 = new MayW2();
// w2.leastBricks(new ArrayList<>());
ListNode head = new ListNode(3);
head.next = new ListNode(1);
head.next.next = new ListNode(4);
head.next.next.next = new ListNode(2);
// w2.sortList(head);
List<String> list1 = Arrays.asList("John", "johnsmith@mail.com", "john00@mail.com");
List<String> list2 = Arrays.asList("John", "johnnybravo@mail.com");
List<String> list3 = Arrays.asList("John", "johnsmith@mail.com", "john_newyork@mail.com");
List<String> list4 = Arrays.asList("Mary", "mary@mail.com");
List<List<String>> accounts = new ArrayList<>();
accounts.add(list1);
accounts.add(list2);
accounts.add(list3);
accounts.add(list4);
w2.accountsMerge(accounts);
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class JulyW1 method sortedListToBST.
/* LC : 109. Convert Sorted List to Binary Search Tree
* https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
* https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* Time : O(nlogn)
*/
public TreeNode sortedListToBST(ListNode head) {
if (head == null)
return null;
if (head.next == null)
return new TreeNode(head.val);
ListNode mid = null;
ListNode fast = head, slow = head;
ListNode left = head, right = head;
ListNode prevToMid = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
prevToMid = slow;
slow = slow.next;
}
mid = slow;
if (mid != head) {
prevToMid.next = null;
} else {
left = null;
}
right = mid.next;
TreeNode root = new TreeNode(mid.val);
root.left = sortedListToBST(left);
root.right = sortedListToBST(right);
return root;
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class JulyW3 method partition.
/*
86. Partition List
https://leetcode.com/problems/partition-list/ */
public ListNode partition(ListNode head, int x) {
ListNode smallHead = new ListNode(0);
ListNode biggerHead = new ListNode(0);
ListNode smaller = smallHead;
ListNode bigger = biggerHead;
while (head != null) {
if (head.val < x) {
smaller.next = head;
smaller = smaller.next;
} else {
bigger.next = head;
bigger = bigger.next;
}
head = head.next;
}
smaller.next = biggerHead.next;
bigger.next = null;
return smallHead.next;
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class FebruaryW2 method reverseKGroupRec.
public ListNode reverseKGroupRec(ListNode head, int k) {
ListNode fastPtr = head;
int i = 0;
for (; i < k && fastPtr != null; i++) {
fastPtr = fastPtr.next;
}
if (i == k) {
fastPtr = reverseKGroupRec(fastPtr, k);
while (i-- > 0) {
ListNode next = head.next;
head.next = fastPtr;
fastPtr = head;
head = next;
}
head = fastPtr;
}
return head;
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class NovemberW3 method swapRecEasy.
public ListNode swapRecEasy(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode n = head.next;
head.next = swapRecEasy(head.next.next);
n.next = head;
return n;
}