Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testrun;
- public class LinkedList {
- public static void main(String[] args) {
- Node start = new Node(1);
- start.next = new Node(2);
- start.next.next = new Node(3);
- start.next.next.next = new Node(4);
- start.next.next.next.next = new Node(5);
- // 1's random points to 3
- start.random = start.next.next;
- // 2's random points to 1
- start.next.random = start;
- // 3's and 4's random points to 5
- start.next.next.random = start.next.next.next.next;
- start.next.next.next.random = start.next.next.next.next;
- // 5's random points to 2
- start.next.next.next.next.random = start.next;
- System.out.println("Original list : ");
- printList(start);
- cloneLinkedListWithNextAndRandomPointer(start);
- // Node head2 = new Node(0);
- // head2.next = new Node(3);
- // head2.next.next = new Node(12);
- // head2.next.next.next = new Node(32);
- // head2.next.next.next.next = new Node(90);
- // head2.next.next.next.next.next = new Node(100);
- // head2.next.next.next.next.next.next = new Node(120);
- // head2.next.next.next.next.next.next.next = new Node(130);
- // printMaxPathLL(head, head2);
- // addTwoLL(head, head2);
- // head.next.next.next.next = new Node(5);
- // head.next.next.next.next.next = new Node(6);
- // head.next.next.next.next.next = new Node(6);
- // printList(head);
- // KthElementFromLast(head, 2);
- // head = rearrangeAtOddEven(head);
- // printList(head);
- // reverseLLIterative(head);
- // pairWiseSwap(head);
- // sunOfTwoLinkedList(head, head2);
- // boolean isPalin = isPalindrom(head);
- // System.out.print("is Palindrom: " + isPalin);
- }
- public static void cloneLinkedListWithNextAndRandomPointer(Node original) {
- if (original == null) {
- return;
- }
- Node headCopy = original;
- while (headCopy != null) {
- Node curCopy = new Node(headCopy.data);
- Node temp = headCopy.next;
- headCopy.next = curCopy;
- curCopy.next = temp;
- headCopy = temp;
- }
- // printList(original);
- headCopy = original;
- while (headCopy != null) {
- if (headCopy.next != null) {
- headCopy.next.random = headCopy.random != null ? headCopy.random.next : headCopy.random;
- }
- headCopy = headCopy.next.next;
- }
- headCopy = original;
- Node copyHead = original.next;
- Node copy = original.next;
- while (headCopy != null && headCopy.next != null && copy != null && copy.next != null) {
- headCopy = headCopy.next.next;
- copy = copy.next.next;
- }
- printList(original);
- printList(copyHead);
- }
- public static void printMaxPathLL(Node head1, Node head2) {
- if (head1 == null && head1 == null) {
- return;
- }
- printList(head1);
- printList(head2);
- Node prevA = head1, prevB = head2, a = head1, b = head2;
- Node result = null;
- while (a != null || b != null) {
- int sumA = 0;
- int sumB = 0;
- while (a != null && b != null && a.data != b.data) {
- if (a.data < b.data) {
- sumA += a.data;
- a = a.next;
- } else {
- sumB += b.data;
- b = b.next;
- }
- }
- if (a == null) {
- System.out.print("First");
- while (b != null) {
- sumA += b.data;
- b = b.next;
- }
- }
- if (b == null) {
- while (a != null) {
- sumB += a.data;
- a = a.next;
- }
- }
- System.out.println("sumA: " + sumA);
- System.out.println("sumB: " + sumB);
- System.out.println("prevB: " + prevB.data);
- System.out.println("prevA: " + prevA.data);
- if (prevA == head1 && prevB == head2) {
- result = (sumA > sumB) ? prevA : prevB;
- } else {
- if (sumA > sumB) {
- prevB.next = prevA.next;
- } else {
- prevA.next = prevB.next;
- }
- }
- prevA = a;
- prevB = b;
- if (a != null) {
- a = a.next;
- }
- if (b != null) {
- b = b.next;
- }
- System.out.print("Result list");
- printList(result);
- }
- // printList(result);
- }
- static Node thirdLL = null;
- static int s_carry = 0;
- public static int addSameSize(Node a, Node b) {
- if (a == null) {
- return 0;
- }
- int carry = addSameSize(a.next, b.next);
- int sum = a.data + b.data + carry;
- carry = sum / 10;
- sum = sum % 10;
- if (thirdLL == null) {
- thirdLL = new Node(sum);
- } else {
- Node temp = new Node(sum);
- temp.next = thirdLL;
- thirdLL = temp;
- }
- return carry;
- }
- public static void addTwoLL(Node a, Node b) {
- if (a == null && b == null) {
- return;
- }
- int size1 = lengthOfLL(a);
- int size2 = lengthOfLL(b);
- printList(a);
- printList(b);
- if (size1 == size2) {
- addSameSize(a, b);
- } else {
- int diff = Math.abs(size1 - size2);
- Node biggerListHead = a;
- Node temp = a;
- Node other = b;
- if (size2 > size1) {
- biggerListHead = b;
- temp = b;
- other = a;
- }
- int count = 0;
- while (count < diff) {
- temp = temp.next;
- count++;
- }
- s_carry = addSameSize(temp, other);
- fillbiggerList(biggerListHead, diff, 0);
- }
- printList(thirdLL);
- }
- public static void fillbiggerList(Node a, int diff, int cutCount) {
- if (a == null || cutCount >= diff) {
- return;
- }
- fillbiggerList(a.next, diff, cutCount + 1);
- int sum = a.data + s_carry;
- s_carry = sum / 10;
- sum = sum % 10;
- if (thirdLL == null) {
- thirdLL = new Node(sum);
- } else {
- Node temp = new Node(sum);
- temp.next = thirdLL;
- thirdLL = temp;
- }
- }
- public static void sunOfTwoLinkedList(Node headOne, Node headTwo) {
- // edge cases
- if (headOne == null && headTwo == null) {
- return;
- }
- if (headOne == null && headTwo != null) {
- printList(headTwo);
- return;
- }
- if (headOne != null && headTwo == null) {
- printList(headOne);
- return;
- }
- printList(headOne);
- printList(headTwo);
- headOne = reverseLLIterative(headOne);
- headTwo = reverseLLIterative(headTwo);
- Node resultHead = null;
- int carry = 0;
- while (headOne != null && headTwo != null) {
- int sum = headOne.data + headTwo.data + carry;
- if (resultHead == null) {
- resultHead = new Node(sum % 10);
- } else {
- Node temp = new Node(sum % 10);
- temp.next = resultHead;
- resultHead = temp;
- }
- carry = sum / 10;
- headOne = headOne.next;
- headTwo = headTwo.next;
- }
- while (headOne != null) {
- int sum = headOne.data + carry;
- Node temp = new Node(sum % 10);
- temp.next = resultHead;
- resultHead = temp;
- headOne = headOne.next;
- carry = sum / 10;
- }
- while (headTwo != null) {
- int sum = headTwo.data + carry;
- Node temp = new Node(sum % 10);
- temp.next = resultHead;
- resultHead = temp;
- headTwo = headTwo.next;
- carry = sum / 10;
- }
- if (carry > 0) {
- Node temp = new Node(carry);
- temp.next = resultHead;
- resultHead = temp;
- }
- System.out.println("Sum Linked List:");
- printList(resultHead);
- }
- public static void pairWiseSwap(Node head) {
- System.out.println("Pair Wise swap: ");
- if (head == null) {
- return;
- }
- Node curNode = head;
- while (curNode != null && curNode.next != null) {
- int data = curNode.data;
- curNode.data = curNode.next.data;
- curNode.next.data = data;
- curNode = curNode.next.next;
- }
- printList(head);
- }
- public static Node reverseLLRecusrive(Node head) {
- if (head == null) {
- return null;
- }
- if (head.next == null) {
- return head;
- }
- Node newHead = reverseLLRecusrive(head.next);
- head.next.next = head;
- head.next = null;
- return newHead;
- }
- static Node reverseLLForGivenK(Node head, int k) {
- Node current = head;
- Node next = null;
- Node prev = null;
- int count = 0;
- while (count < k && current != null) {
- next = current.next;
- current.next = prev;
- prev = current;
- current = next;
- count++;
- }
- if (next != null) {
- head.next = reverseLLForGivenK(next, k);
- }
- return prev;
- }
- public static void rotateAtKthPosition(Node head, int k) {
- if (head == null) {
- return;
- }
- int count = 1;
- Node curNode = head;
- while (count < k && curNode != null) {
- curNode = curNode.next;
- count++;
- }
- if (curNode == null) {
- return;
- }
- Node kthnode = curNode;
- while (curNode.next != null) {
- curNode = curNode.next;
- }
- curNode.next = head;
- head = kthnode.next;
- kthnode.next = null;
- printList(head);
- }
- public static boolean isPalindrom(Node head) {
- Node middleNode = printMiddleOfLL(head);
- if (middleNode != null) {
- middleNode = reverseLLIterative(middleNode);
- Node firstHalfHead = head;
- Node secondHalfNode = middleNode;
- while (firstHalfHead != null && secondHalfNode != null) {
- if (firstHalfHead.data != secondHalfNode.data) {
- return false;
- }
- firstHalfHead = firstHalfHead.next;
- secondHalfNode = secondHalfNode.next;
- }
- }
- return true;
- }
- public static Node reverseLLIterative(Node head) {
- Node prev = null;
- Node next = null;
- Node cur = head;
- while (cur != null) {
- next = cur.next;
- cur.next = prev;
- prev = cur;
- cur = next;
- }
- head = prev;
- return head;
- }
- public static int lengthOfLL(Node head) {
- Node temp = head;
- int size = 0;
- while (temp != null) {
- size++;
- temp = temp.next;
- }
- System.out.println("Size of LL: " + size);
- return size;
- }
- public static void KthElementFromLast(Node head, int k) {
- Node temp = head;
- int length = lengthOfLL(temp);
- if (length < k) {
- return;
- }
- temp = head;
- int indexFrombegining = length - k + 1;
- for (int i = 1; i < indexFrombegining; i++) {
- temp = temp.next;
- }
- System.out.print("Element from " + k + "th index from end: " + temp.data);
- }
- public static Node printMiddleOfLL(Node head) {
- if (head == null) {
- return null;
- }
- Node temp = head;
- Node fastNode = temp;
- Node slowNode = temp;
- while (fastNode != null && fastNode.next != null) {
- fastNode = fastNode.next.next;
- slowNode = slowNode.next;
- }
- System.out.println("Mid of LL: " + slowNode.data);
- return slowNode;
- }
- public static Node splitListIntoTwoHalf(Node head) {
- if (head == null) {
- return null;
- }
- Node temp = head;
- Node fastNode = temp;
- Node slowNode = temp;
- while (fastNode != null && fastNode.next != null) {
- fastNode = fastNode.next.next;
- slowNode = slowNode.next;
- }
- fastNode.next = null;
- System.out.println("Mid of LL: " + slowNode.data);
- return slowNode;
- }
- public static Node rearrangeAtOddEven(Node head) {
- if (head == null) {
- return null;
- }
- Node oddNode = head, evenNode = head.next, preEvenNode = evenNode;
- while (true) {
- // break condition ,if LL reaches at end or even or next to even are
- // null,connect odd position list to first even node
- if (oddNode == null || evenNode == null || evenNode.next == null) {
- oddNode.next = preEvenNode;
- break;
- }
- // rearrange pointers for example connect first to thrid
- oddNode.next = evenNode.next;
- // increment odd node by skipping one
- oddNode = evenNode.next;
- // if reaches at end,connect first even node to odd list
- if (oddNode.next == null) {
- evenNode.next = null;
- oddNode.next = preEvenNode;
- }
- // rearrange evenNode pointers
- evenNode.next = oddNode.next;
- // increment even node by skipping one
- evenNode = oddNode.next;
- }
- return head;
- }
- public boolean detectCycle(Node head) {
- if (head == null || head.next == null) {
- return false;
- }
- Node fast = head;
- Node slow = head;
- while (slow != null && fast != null && fast.next != null) {
- fast = fast.next.next;
- slow = slow.next;
- if (fast != null && slow != null) {
- if (fast == slow) {
- return true;
- }
- }
- }
- return false;
- }
- public static Node mergePointINtwoLL(Node head1, Node head2) {
- if (head1 == null && head2 == null) {
- return null;
- }
- int lengthOfFirstLL = lengthOfLL(head1);
- int lengthOfSecondLL = lengthOfLL(head2);
- int diffrence = Math.abs(lengthOfFirstLL - lengthOfSecondLL);
- Node firstNode = null, secondNode = null;
- if (lengthOfFirstLL > lengthOfSecondLL) {
- firstNode = head1;
- secondNode = head2;
- } else {
- firstNode = head2;
- secondNode = head1;
- }
- int count = 0;
- while (count < diffrence) {
- firstNode = firstNode.next;
- }
- while (firstNode != null && secondNode != null) {
- if (firstNode == secondNode) {
- return firstNode;
- }
- firstNode = firstNode.next;
- secondNode = secondNode.next;
- }
- return null;
- }
- public static void printList(Node head) {
- Node temp = head;
- while (temp != null) {
- System.out.print(temp.data + "->");
- temp = temp.next;
- }
- System.out.println("NULL");
- }
- public static Node mergeTwoSortedLL(Node a, Node b) {
- Node resultLL = null;
- if (a == null && b == null) {
- return null;
- }
- if (a == null) {
- return b;
- }
- if (b == null) {
- return a;
- }
- if (a.data > b.data) {
- resultLL = b;
- resultLL.next = mergeTwoSortedLL(a, b.next);
- } else {
- resultLL = a;
- resultLL.next = mergeTwoSortedLL(a.next, b);
- }
- return resultLL;
- }
- public static Node mergeSort(Node head) {
- if (head == null || head.next == null) {
- return head;
- }
- Node mid = printMiddleOfLL(head);
- Node midNext = mid.next;
- // set null to next to mid to split the list
- mid.next = null;
- Node leftList = mergeSort(head);
- Node rightList = mergeSort(midNext);
- return mergeTwoSortedLL(leftList, rightList);
- }
- static class Node {
- int data;
- Node next;
- Node random;
- public Node() {
- super();
- }
- public Node(int data) {
- super();
- this.data = data;
- this.next = null;
- }
- public Node(int data, Node next) {
- super();
- this.data = data;
- this.next = next;
- }
- public int getData() {
- return data;
- }
- public void setData(int data) {
- this.data = data;
- }
- public Node getNext() {
- return next;
- }
- public void setNext(Node next) {
- this.next = next;
- }
- public void setRandom(Node r) {
- this.random = r;
- }
- }
- }
Add Comment
Please, Sign In to add comment