use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class A method arrayToLinkedList.
ListNode arrayToLinkedList(int[] arr) {
int n = arr.length;
ListNode root = null;
for (int i = 0; i < n; i++) {
root = insert(root, arr[i]);
}
return root;
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class MarchW3 method detectCycle.
/* https://leetcode.com/problems/linked-list-cycle-ii/ */
public ListNode detectCycle(ListNode head) {
if (head == null)
return null;
ListNode tortoise = head;
ListNode hare = head;
while (hare != null && hare.next != null) {
tortoise = tortoise.next;
hare = hare.next.next;
if (tortoise == hare) {
break;
}
}
if (hare == null || hare.next == null)
return null;
tortoise = head;
while (tortoise != hare) {
tortoise = tortoise.next;
hare = hare.next;
}
return tortoise;
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class MarchW4 method helper.
public ListNode helper(Stack<Integer> s1, Stack<Integer> s2) {
while (!s1.isEmpty() || !s2.isEmpty()) {
if (!s1.isEmpty())
sum += s1.pop();
if (!s2.isEmpty())
sum += s2.pop();
head.val = sum % 10;
ListNode n = new ListNode(sum / 10);
n.next = head;
head = n;
sum /= 10;
}
return head.val == 0 ? head.next : head;
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class MarchW4 method addTwoNumbers.
/* https://leetcode.com/problems/add-two-numbers-ii/ */
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int len1 = 0, len2 = 0;
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
ListNode t1 = l1, t2 = l2;
while (l1 != null) {
s1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
s2.push(l2.val);
l2 = l2.next;
}
return helper(s1, s2);
}
use of com.pkumar7.datastructures.ListNode in project Data-Structures-Algorithms by pankajgangwar.
the class MayW2 method mergeSortedListIterative.
/* https://leetcode.com/problems/merge-two-sorted-lists/ */
public ListNode mergeSortedListIterative(ListNode l1, ListNode l2) {
ListNode a = l1;
ListNode b = l2;
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while (a != null && b != null) {
if (a.val < b.val) {
dummy.next = a;
a = a.next;
} else {
dummy.next = b;
b = b.next;
}
dummy = dummy.next;
}
if (a != null) {
dummy.next = a;
}
if (b != null) {
dummy.next = b;
}
return head.next;
}