diff --git a/Add2LL.java b/Add2LL.java new file mode 100644 index 0000000..8d9375c --- /dev/null +++ b/Add2LL.java @@ -0,0 +1,79 @@ +package LinkedList; + +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class Add2LL { + public static void main(String[] args) { + LinkedList l1 = new LinkedList(); + LinkedList l2 = new LinkedList(); + l1.addLast(1); + l2.addLast(1); + l2.addLast(9); + l2.addLast(9); + l2.addLast(9); + Node ans = add(l1.head, l2.head); + display(ans); + } + static void display(Node node){ + while(node!=null){ + System.out.print(node.data+" "); + node = node.next; + } + System.out.println(); + } + static Node add(Node n1, Node n2){ + n1 = reverse(n1); + n2 = reverse(n2); + int s1 = getSize(n1); + int s2 = getSize(n2); + if(s1>s2){ + add0(n2,s1-s2); + }else{ + add0(n1,s2-s1); + } + int carry = 0; + Node tail = n1; + // display(n1); + // display(n2); + while(n1!=null){ + int temp = n1.data; + n1.data = (n2.data+temp+carry)%10; + carry = (n2.data+temp+carry)/10; + if(n1.next==null && carry!=0){ + n1.next = new Node(carry); + break; + } + n1 = n1.next; + n2 = n2.next; + } + return reverse(tail); + + } + static void add0(Node n, int s){ + Node temp = n; + while(temp.next!=null){ + temp = temp.next; + } + while(s>0){ + temp.next = new Node(0); + temp = temp.next; + s--; + } + } + static int getSize(Node n1){ + int ans =0 ; + while(n1!=null){ + n1 = n1.next; + ans++; + } + return ans; + } + static Node reverse(Node n1){ + if(n1==null||n1.next==null) return n1; + Node node = reverse(n1.next); + n1.next.next = n1; + n1.next=null; + return node; + } + +} diff --git a/AddInADoublyLL.java b/AddInADoublyLL.java new file mode 100644 index 0000000..0ce9be3 --- /dev/null +++ b/AddInADoublyLL.java @@ -0,0 +1,36 @@ +package LinkedList; +import LinkedList.LinkedList.*; +public class AddInADoublyLL { + public static void main(String[] args) { + + } + static void insert(DLL head_ref, int x, int data){ + DLL node = new DLL(data); + if(head_ref==null){ + head_ref = node; + } + if(x==0){ + node.next = head_ref; + head_ref.prev = node; + head_ref = node; + } + DLL temp = head_ref; + while(temp!=null && x>0){ + temp = temp.next; + x--; + } + if(temp!=null){ + if(temp.next==null){ + node.prev = temp; + temp.next = node; + } + else{ + temp.next.prev = node; + node.next = temp.next; + node.prev = temp; + temp.next = node; + // node.next.prev = node; + } + } + } +} diff --git a/DLL.java b/DLL.java new file mode 100644 index 0000000..5a621a1 --- /dev/null +++ b/DLL.java @@ -0,0 +1,11 @@ +package LinkedList.LinkedList; + +public class DLL { + public int data; + public DLL prev; + public DLL next; + public DLL(int data){ + this.data = data; + prev = next = null; + } +} diff --git a/DeleteAndReverseInLL.java b/DeleteAndReverseInLL.java new file mode 100644 index 0000000..5c8d8c0 --- /dev/null +++ b/DeleteAndReverseInLL.java @@ -0,0 +1,48 @@ +package LinkedList; + +import LinkedList.LinkedList.*; +public class DeleteAndReverseInLL { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(2); + ll.addLast(5); + ll.addLast(7); + ll.addLast(8); + ll.addLast(10); + ll.tail.next = ll.head; + ll.head = delete(ll.head, 8); + display(ll.head); + } + static void display(Node node){ + Node temp = node; + while(temp.next!=node){ + System.out.print(temp.data+"->"); + temp = temp.next; + } + System.out.print(temp.data+" "); + + } + static Node delete(Node node, int data){ + if(node==null) return null; + if(node.data==data && node.next==null) return null; + Node temp = node; + while(temp.next!=null && temp.next.data!=data){ + temp = temp.next; + } + temp.next = temp.next.next; + Node ans = reverse(node); + node.next = ans; + return ans; + + } + static Node reverse(Node head){ + return reverseHelper(head,head); + } + static Node reverseHelper(Node node, Node head){ + if(node.next == head) return node; + Node ans = reverseHelper(node.next, head); + node.next.next = node; + node.next = null; + return ans; + } +} diff --git a/DeleteMidOfLL.java b/DeleteMidOfLL.java new file mode 100644 index 0000000..e72f52f --- /dev/null +++ b/DeleteMidOfLL.java @@ -0,0 +1,29 @@ +package LinkedList; + +import LinkedList.LinkedList.*; +public class DeleteMidOfLL { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(1); + ll.addLast(2); + ll.addLast(3); + ll.addLast(4); + ll.addLast(5); + ll.addLast(6); + ll.head = delete(ll.head); + ll.display(); + } + static Node delete(Node node){ + if(node==null||node.next==null) return null; + Node slow = node; + Node fast = node; + Node tail = slow; + while(fast!=null && fast.next!=null){ + fast = fast.next.next; + tail = slow; + slow = slow.next; + } + tail.next = slow.next; + return node; + } +} diff --git a/DeleteNNodesAfterMNodes.java b/DeleteNNodesAfterMNodes.java new file mode 100644 index 0000000..8a35683 --- /dev/null +++ b/DeleteNNodesAfterMNodes.java @@ -0,0 +1,49 @@ +package LinkedList; + +import LinkedList.LinkedList.*; +public class DeleteNNodesAfterMNodes { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(9); + ll.addLast(1); + ll.addLast(3); + ll.addLast(5); + ll.addLast(9); + ll.addLast(4); + ll.addLast(10); + ll.addLast(1); + ll.head = delete(ll.head, 2, 1); + ll.display(); + } + static Node delete(Node node , int m, int n){ + if(m==0 && n==0) return node; + if(m==0) return null; + Node head = node; + while(head!=null){ + int skip = m; + // skipping + while(head!= null && skip>1){ + head = head.next; + skip--; + } + Node prev = head; + if(head!=null){ + head = head.next; + } + int del = n; + //delting + while(head!=null && del>1){ + head = head.next; + del--; + } + if(head!=null){ + Node next = head.next; + head = head.next; + prev.next = next; + }else{ + prev.next = null; + } + } + return node; + } +} diff --git a/DeleteNodesHavingGreaterValueOnRight.java b/DeleteNodesHavingGreaterValueOnRight.java new file mode 100644 index 0000000..b73974e --- /dev/null +++ b/DeleteNodesHavingGreaterValueOnRight.java @@ -0,0 +1,27 @@ +package LinkedList; +import LinkedList.LinkedList.*; +public class DeleteNodesHavingGreaterValueOnRight { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(12); + ll.addLast(15); + ll.addLast(10); + ll.addLast(11); + ll.addLast(5); + ll.addLast(6); + ll.addLast(2); + ll.addLast(3); + ll.head = delete(ll.head); + ll.display(); + } + static Node delete(Node node){ + //elegant solution + if(node==null||node.next==null) return node; + Node temp = delete(node.next);//returns max node value on right + if(temp.data>node.data) return temp; + else{ + node.next = temp; + return node; + } + } +} diff --git a/DeleteWithoutHeadPointer.java b/DeleteWithoutHeadPointer.java new file mode 100644 index 0000000..863cf2f --- /dev/null +++ b/DeleteWithoutHeadPointer.java @@ -0,0 +1,12 @@ +package LinkedList; + +import LinkedList.LinkedList.*; +public class DeleteWithoutHeadPointer { + public static void main(String[] args) { + + } + static void delete(Node node){ + node.data = node.next.data; + node.next = node.next.next; + } +} diff --git a/DeletingNodesInDoublyLinkedList.java b/DeletingNodesInDoublyLinkedList.java new file mode 100644 index 0000000..c9b02c7 --- /dev/null +++ b/DeletingNodesInDoublyLinkedList.java @@ -0,0 +1,32 @@ +package LinkedList; + +import LinkedList.LinkedList.*; +public class DeletingNodesInDoublyLinkedList { + public static void main(String[] args) { + + } + static DLL delete(DLL node, int x){ + { + // Your code here + DLL temp = node; + if(node==null) return null; + while(node!=null && x>1){ + node = node.next; + x--; + } + if(node!=null && node.next!=null && node.prev!=null){ + DLL t = node.prev; + node.prev.next = node.next; + node.next.prev = t; + } + else if(node.prev==null){ + temp = node.next; + node.next=null; + } + else if(node.next==null){ + node.prev.next = null; + } + return temp; + + } +}} diff --git a/DesignAPhoneDirectory.java b/DesignAPhoneDirectory.java new file mode 100644 index 0000000..c192a36 --- /dev/null +++ b/DesignAPhoneDirectory.java @@ -0,0 +1,45 @@ +package LinkedList; + +import java.util.HashSet; +import java.util.LinkedList; +public class DesignAPhoneDirectory { + /* + * Design a Phone Directory which supports the following operations: + + get: Provide a number which is not assigned to anyone. + check: Check if a number is available or not. + release: Recycle or release a number. + */ + int max; + HashSet set; + LinkedList queue; + /** + * + * @param maxNumbers - Maximum numbers that can be stored in the phone Directory + */ + public DesignAPhoneDirectory(int maxNumbers){ + set = new HashSet<>(); + queue = new LinkedList<>(); + for(int i=0;isize/2){ + Node temp = left.next; + left.next = right; + right.next = temp; + left = temp; + }else if(level==size/2){ + left=right; + } + } + + +} diff --git a/InsertInMiddleOfLinkedList.java b/InsertInMiddleOfLinkedList.java new file mode 100644 index 0000000..8e342f1 --- /dev/null +++ b/InsertInMiddleOfLinkedList.java @@ -0,0 +1,29 @@ +package LinkedList; + import LinkedList.LinkedList.*; +public class InsertInMiddleOfLinkedList { + public static void main(String[] args) { + + } + static Node insertInMid(Node head, int data){ + //Insert code here, return the head of modified linked list\ + Node node = new Node(data); + if(head==null){ + head = node; + return head; + } + if(head.next==null){ + head.next = node; + return head; + } + Node fast = head; + Node slow = head; + while(fast.next!=null && fast.next.next!=null){ + fast = fast.next.next; + slow = slow.next; + } + Node temp = slow.next; + slow.next = node; + node.next = temp; + return head; + } +} diff --git a/InsertInSortedLinkedList.java b/InsertInSortedLinkedList.java new file mode 100644 index 0000000..c775e47 --- /dev/null +++ b/InsertInSortedLinkedList.java @@ -0,0 +1,42 @@ +package LinkedList; +import LinkedList.LinkedList.*; +public class InsertInSortedLinkedList { + public static void main(String[] args) { + + } + public static Node sortedInsert(Node head,int data) + { + //add code here. + Node node = new Node(data); + if(head==null){ + head = node; + head.next = head; + return head; + } + Node temp = head; + while(temp.next!=head){ + temp = temp.next; + } + if(head.data>=data){ + node.next = head; + head = node; + temp.next = head; + return head; + } + temp = head; + while(temp.next!=head && temp.data<=data && temp.next.data<=data){ + temp = temp.next; + } + if(temp.next==head){ + temp.next = node; + node.next = head; + } + else{ + Node t = temp.next; + temp.next = node; + node.next =t; + } + return head; + + } +} diff --git a/IsCyclePresent.java b/IsCyclePresent.java new file mode 100644 index 0000000..8e15e5b --- /dev/null +++ b/IsCyclePresent.java @@ -0,0 +1,21 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +public class IsCyclePresent { + public static void main(String[] args) { + + } + static boolean hasCycle(Node node){ + if(node==null||node.next == null) return false; + Node fast = node; + Node slow = node; + while(fast!=null && fast.next!=null){ + slow = slow.next; + fast = fast.next.next; + if(slow==fast){ + return true; + } + } + return false; + + } +} diff --git a/KReverse.java b/KReverse.java new file mode 100644 index 0000000..dc9ba41 --- /dev/null +++ b/KReverse.java @@ -0,0 +1,76 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; + + +public class KReverse { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(1); + ll.addLast(2); + ll.addLast(3); + ll.addLast(4); + ll.addLast(5); + ll.addLast(6); + ll.addLast(7); + ll.addLast(8); + ll.addLast(9); + ll.head = kReverse(ll.head, 2); + display(ll.head); + + } + static void display(Node node){ + while(node!=null){ + System.out.print(node.data+" "); + node = node.next; + } + } + static int length(Node node){ + int ans=0; + while(node!=null){ + ans++; + node=node.next; + } + return ans; + } + static Node th = null; + static Node tt = null; + static void addFirstNode(Node node){ + if(th==null){ + th=node; + tt = node; + }else{ + node.next = th; + th = node; + } + } + static Node kReverse(Node head, int k){ + if(head==null||head.next==null||k<=0) return head; + // if k==1, reverse the whole list + int len = length(head); + Node oh = null, ot= null; + Node curr = head; + while(len>=k){ + int tempK = k; + while(tempK>0){ + Node forw = curr.next; + curr.next = null; + addFirstNode(curr); + curr = forw; + tempK--; + } + len-=k; + if(oh==null){ + oh = th; + ot = tt; + }else{ + ot.next = th; + ot = tt; + } + th = null; + tt = null; + } + ot.next = curr;// whatever's left + return oh; + } +} diff --git a/KReverseVariantII.java b/KReverseVariantII.java new file mode 100644 index 0000000..4210ec4 --- /dev/null +++ b/KReverseVariantII.java @@ -0,0 +1,23 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +public class KReverseVariantII { + public static void main(String[] args) { + + } + static Node reverse(Node head, int k){ + if(head==null||head.next==null) return head; + Node curr = head; + Node next = head; + Node prev = null; + int count = 0; + while(count map; + Entry start, end; + int LRU_SIZE = 4; + public LRU(){ + map = new HashMap<>(); + } + public int getEntry(int key){ + if(map.containsKey(key)){ + Entry entry = map.get(key); + removeNode(entry); + addAtTop(entry); + return entry.value; + } + return -1; + } + public void putEntry(int key, int value){ + if(map.containsKey(key)){ + Entry entry = map.get(key); + entry.value = value; + removeNode(entry); + addAtTop(entry); + }else{ + Entry node = new Entry(); + node.left = null; + node.right = null; + node.value = value; + node.key = key; + if(map.size()>LRU_SIZE){ + map.remove(end.key); + removeNode(end); + addAtTop(node); + }else{ + addAtTop(node); + } + map.put(key, node); + } + } + public void addAtTop(Entry node){ + node.right = start; + node.left = null; + if(start!=null){ + start.left = node; + } + start = node; + if(end==null){ + end = start; + } + + } + public void removeNode(Entry node){ + if(node.left!=null){ + node.left.right = node.right; + } + else{ + start = node.right; + } + if(node.right!=null){ + node.right.left = node.left; + }else{ + end = node.left; + } + } +} diff --git a/LinkedList.java b/LinkedList.java new file mode 100644 index 0000000..f06e175 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,121 @@ +package LinkedList.LinkedList; + + +public class LinkedList { + public Node head,tail; + int size; + public LinkedList(){ + head=tail=null; + size=0; + } + // add + // Add First + public void addFirst(int data){ + Node node = new Node(data); + size++; + if(head==null){ + tail = head = node; + return; + } + node.next = head; + head = node; + } + public void addLast(int data){ + Node node = new Node(data); + size++; + if(head==null){ + head = tail = node; + return; + } + tail.next = node; + tail = node; + } + public void addPosition(int data, int pos) throws Exception{ + if(pos>size){ + throw new Exception("The Linked List doesn't have these many notes"); + } + if(head==null && pos!=0){ + throw new Exception("The linked list is Empty, can't insert at that position"); + } + if(head==null){ + addFirst(data); + return; + } + if(pos==size){ + addLast(data); + return; + } + Node node = new Node(data); + Node temp = head; + while(pos>1){ + temp = temp.next; + pos--; + } + node.next = temp.next; + temp.next = node; + size++; + } + // delete + public void deleteFirst() throws Exception{ + if(head==null){ + throw new Exception("Linked List is Empty"); + } + head = head.next; + size--; + } + public void deleteLast() throws Exception{ + if(head==null){ + throw new Exception("Linked List is Empty"); + } + Node temp = head; + while(temp.next!=tail){ + temp = temp.next; + } + tail = temp; + tail.next = null; + size--; + } + public void deleteAtPosition(int pos) throws Exception{ + if(head==null){ + throw new Exception("List is Empty"); + } + if(pos<0 || pos>=size){ + throw new Exception("Invalid Index"); + } + Node temp = head; + if(pos==0){ + deleteFirst(); + return; + } + if(pos==size-1){ + deleteLast(); + return; + } + while(pos>1){ + temp= temp.next; + pos--; + } + temp.next = temp.next.next; + } + // size + public int size(){ + return this.size; + } + // display + public void display(){ + Node temp = head; + while(temp!=null){ + System.out.print(temp.data+" "); + temp = temp.next; + } + System.out.println(); + } + public Node get(int x){ + Node temp = head; + while(x>0){ + x--; + temp = temp.next; + } + return temp; + } +} diff --git a/LinkedListThatIsSortedAlternately.java b/LinkedListThatIsSortedAlternately.java new file mode 100644 index 0000000..9f69d89 --- /dev/null +++ b/LinkedListThatIsSortedAlternately.java @@ -0,0 +1,51 @@ +package LinkedList; + +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class LinkedListThatIsSortedAlternately { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(1); + ll.addLast(9); + ll.addLast(2); + ll.addLast(8); + ll.addLast(3); + ll.addLast(7); + ll.addLast(4); + ll.head = sort(ll.head); + ll.display(); + } + static Node sort(Node node){ + if(node==null||node.next==null||node.next.next==null) return node; + Node h1 = node; + Node t1 = node; + Node h2 = node.next; + Node t2 = node.next; + int level = 0; + node = node.next.next; + while(node!=null){ + Node next = node.next; + if(level%2==0){ + t1.next = node; + t1 = t1.next; + }else{ + t2.next = node; + t2 = t2.next; + } + node = next; + level++; + } + t2.next=null; + h2 = reverseRecuriveHelp(h2); + t1.next = h2; + return h1; + } + + static Node reverseRecuriveHelp(Node node){ + if(node==null||node.next==null) return node; + Node head = reverseRecuriveHelp(node.next); + node.next.next = node; + node.next = null; + return head; + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..b360044 --- /dev/null +++ b/Main.java @@ -0,0 +1,19 @@ +package LinkedList.LinkedList; + +public class Main { + public static void main(String[] args)throws Exception{ + LinkedList ll = new LinkedList(); + ll.addFirst(12); + // ll.deleteFirst(); + ll.addLast(100); + ll.addFirst(45); + // System.out.println(ll.size); + ll.addPosition(122, 3); + ll.display(); + ll.deleteAtPosition(2); + // System.out.println(ll.size); + // ll.display(); + // ll.deleteLast(); + ll.display(); + } +} diff --git a/Merge2SortedIterative.java b/Merge2SortedIterative.java new file mode 100644 index 0000000..ba1ef84 --- /dev/null +++ b/Merge2SortedIterative.java @@ -0,0 +1,48 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +// import LinkedList.LinkedList.LinkedList; +public class Merge2SortedIterative { + public static void main(String[] args) { + + } + static Node merge(Node h1, Node h2){ + if(h1==null){ + return h2; + } + if(h2==null){ + return h1; + } + if(h1.data=curr1.data && curr2.data<=next1.data){ + next2 = curr2.next; + curr1.next = curr2; + curr2.next = next1; + curr1 = curr2; + curr2 = next2; + }else{ + if(next1.next!=null){ + next1 = next1.next; + curr1 = curr1.next; + }else{ + next1.next = curr2; + return h1; + } + } + } + return h1; + } + +} diff --git a/Merge2SortedLL.java b/Merge2SortedLL.java new file mode 100644 index 0000000..ad7538b --- /dev/null +++ b/Merge2SortedLL.java @@ -0,0 +1,30 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class Merge2SortedLL { + public static void main(String[] args) { + LinkedList ll1 = new LinkedList(); + ll1.addLast(1); + ll1.addLast(3); + ll1.addLast(5); + ll1.addLast(6); + ll1.addLast(8); + LinkedList ll2 = new LinkedList(); + ll2.addLast(2); + ll2.addLast(4); + ll2.addLast(7); + + } + static Node merge(Node n1, Node n2){ + if(n1==null) return n2; + if(n2==null) return n1; + if(n1.data 5*(10^8)*log2(10 ) + static Node mergeKList(Node[] lists){ + if(lists.length==0) return null; + return mergeKList(lists, 0, lists.length-1); + } + static Node mergeKList(Node[] lists, int si, int ei){ + if(ei 10^13 time, wont work + // APPROACH2 - O(N*k*logk) + static Node merge2SortedLL(Node n1,Node n2){ + if(n1==null) return n2; + if(n2==null) return n1; + if(n1.data>n2.data){ + n2.next = merge2SortedLL(n1, n2.next); + return n2; + }else{ + n1.next = merge2SortedLL(n1.next, n2); + return n1; + } + } + static Node merge2SortedLL2(Node n1, Node n2){ + if(n1==null) return n2; + if(n2==null) return n1; + Node dummy = new Node(-1);//head of the new LL + Node prev = dummy;//new LL + Node c1 = n1, c2 = n2; + while(c1!=null && c2!=null){ + if(c1.data pq = new PriorityQueue<>(new Comparator(){ + + @Override + public int compare(Node o1, Node o2) { + return o1.data-o2.data; + } + + }); + for(Node n:l){ + if(n!=null){ + pq.add(n); + } + } + Node head = new Node(0); + Node point = head; + while(!pq.isEmpty()){ + point.next = pq.poll(); + point = point.next; + Node next = point.next; + if(next!=null){ + pq.add(next); + } + } + return head.next; + + } +} diff --git a/MergeListsAlternately.java b/MergeListsAlternately.java new file mode 100644 index 0000000..22a6246 --- /dev/null +++ b/MergeListsAlternately.java @@ -0,0 +1,34 @@ +package LinkedList; + +import LinkedList.LinkedList.LinkedList; +import LinkedList.LinkedList.Node; + +public class MergeListsAlternately { + public static void main(String[] args) { + LinkedList l1 = new LinkedList(); + LinkedList l2 = new LinkedList(); + l1.addLast(1); + l1.addLast(1); + l1.addLast(1); + l1.addLast(1); + l1.addLast(1); + l1.addLast(1); + l2.addLast(2); + l2.addLast(2); + l2.addLast(2); + l2.addLast(2); + l1.head = merge(l1.head, l2.head, 0); + l1.display(); + } + static Node merge(Node n1, Node n2, int level){ + if(n1==null) return n2; + if(n2==null) return n1; + if(level%2==0){ + n1.next = merge(n1.next, n2, level+1); + return n1; + }else{ + n2.next = merge(n1, n2.next, level+1); + return n2; + } + } +} diff --git a/MergeSort.java b/MergeSort.java new file mode 100644 index 0000000..5bd0464 --- /dev/null +++ b/MergeSort.java @@ -0,0 +1,48 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class MergeSort { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addFirst(10); + ll.addFirst(7); + ll.addFirst(1); + ll.addFirst(0); + ll.addFirst(19); + ll.addFirst(5); + Node head = mergeSort(ll.head); + ll.head = head; + ll.display(); + + } + static Node mid(Node node){ + Node slow, fast; + fast = slow = node; + while(fast.next!=null && fast.next.next!=null){ + fast =fast.next.next; + slow = slow.next; + } + return slow; + } + static Node mergeSort(Node node){ + if(node==null||node.next==null) return node; + Node m = mid(node);//mid of Linked list + Node mn = m.next;//next of mid + m.next = null; + Node l = mergeSort(node); + Node r = mergeSort(mn); + Node head = merge(l, r); + return head; + } + static Node merge(Node n1, Node n2){ + if(n1==null) return n2; + if(n2==null) return n1; + if(n1.data0){ + ac.next = new Node(0); + zero--; + ac = ac.next; + } + // display(dummy.next); + return dummy.next; + } + static Node add(Node n1, Node n2){ + n1 = reverse(n1); + n2 = reverse(n2); + int s1 = getSize(n1); + int s2 = getSize(n2); + if(s1>s2){ + add0(n2,s1-s2); + }else{ + add0(n1,s2-s1); + } + int carry = 0; + Node tail = n1; + while(n1!=null){ + int temp = n1.data; + n1.data = (n2.data+temp+carry)%10; + carry = (n2.data+temp+carry)/10; + if(n1.next==null && carry!=0){ + n1.next = new Node(carry); + break; + } + n1 = n1.next; + n2 = n2.next; + } + return reverse(tail); + + } + static void add0(Node n, int s){ + Node temp = n; + while(temp.next!=null){ + temp = temp.next; + } + while(s>0){ + temp.next = new Node(0); + temp = temp.next; + s--; + } + } + static int getSize(Node n1){ + int ans =0 ; + while(n1!=null){ + n1 = n1.next; + ans++; + } + return ans; + } + static Node reverse(Node n1){ + if(n1==null||n1.next==null) return n1; + Node node = reverse(n1.next); + n1.next.next = n1; + n1.next=null; + return node; + } +} diff --git a/Node.java b/Node.java new file mode 100644 index 0000000..49b7edb --- /dev/null +++ b/Node.java @@ -0,0 +1,9 @@ +package LinkedList.LinkedList; +public class Node{ + public int data; + public Node next; + public Node(int data){ + this.data = data; + next = null; + } +} diff --git a/Palindrome.java b/Palindrome.java new file mode 100644 index 0000000..710e5fd --- /dev/null +++ b/Palindrome.java @@ -0,0 +1,26 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class Palindrome { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addFirst(1); + ll.addFirst(2); + ll.addFirst(3); + ll.addFirst(2); + ll.addFirst(1); + head = ll.head; + System.out.println(isPalindrome(ll.head)); + } + static Node head; + static boolean isPalindrome(Node node){ + if(node==null) return true; + if(!isPalindrome(node.next)) return false; + boolean flag = true; + if(head.data!=node.data){ + flag = false; + } + head = head.next; + return flag; + } +} diff --git a/Partition.java b/Partition.java new file mode 100644 index 0000000..391016b --- /dev/null +++ b/Partition.java @@ -0,0 +1,40 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +public class Partition { + public static void main(String[] args) { + + } + static Node partition(Node head, int x){ + if(head==null||head.next==null) return head; + Node h1 = null; + Node t1 = null; + Node h2 = null; + Node t2 = null; + while(head!=null){ + Node next = head.next; + head.next = null; + if(head.data=1){ + fast = fast.next; + k--; + } + while(fast.next!=null){ + fast = fast.next; + slow = slow.next; + } + // System.out.println(slow.data); + if(slow.next!=null){ + slow.next = slow.next.next; + } + return head; + + } + +} diff --git a/Reverse.java b/Reverse.java new file mode 100644 index 0000000..c0301f9 --- /dev/null +++ b/Reverse.java @@ -0,0 +1,43 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class Reverse { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addFirst(5); + ll.addFirst(4); + ll.addFirst(3); + ll.addFirst(2); + ll.addFirst(1); + ll.display(); + reverseRecursive(ll); + ll.display(); + } + static void reverseIterative(LinkedList ll){ + Node prev, curr, next; + prev = null; + curr=next=ll.head; + ll.tail = curr; + while(curr!=null ){ + next = next.next; + curr.next = prev; + prev = curr; + curr = next; + } + ll.head = prev; + + } + static void reverseRecursive(LinkedList ll){ + Node temp = ll.head; + ll.head = reverseRecuriveHelp(ll.head); + ll.tail = temp; + } + static Node reverseRecuriveHelp(Node node){ + if(node==null||node.next==null) return node; + Node head = reverseRecuriveHelp(node.next); + node.next.next = node; + node.next = null; + return head; + } + +} diff --git a/ReverseInRange.java b/ReverseInRange.java new file mode 100644 index 0000000..2debcd1 --- /dev/null +++ b/ReverseInRange.java @@ -0,0 +1,52 @@ +package LinkedList; + +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class ReverseInRange { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(1); + ll.addLast(2); + ll.addLast(3); + ll.addLast(4); + ll.addLast(5); + ll.addLast(6); + ll.addLast(7); + ll.addLast(8); + ll.addLast(9); + ll.head = reverse(ll.head, 3, 7); + display(ll.head); + + } + static void display(Node node){ + while(node!=null){ + System.out.print(node.data+" "); + node = node.next; + } + } + static Node reverse(Node node, int start, int end){ + Node head = null; + Node curr = node; + Node prev = null; + Node next = curr; + int count = 1; + while(countn2.data){ + n1.next = merge(n1.next, n2); + n1.next.prev = n1;//updating previous link of next node; + n1.prev=null;// first node + return n1; + }else{ + n2.next = merge(n1, n2.next); + n2.next.prev = n2;//updating previous link of next node; + n2.prev=null;// first node + return n2; + } + } + static DLL mid(DLL node){ + if(node==null) return null; + DLL fast = node; + DLL slow = node; + while(fast.next!=null && fast.next.next!=null){ + fast = fast.next.next; + slow = slow.next; + } + DLL temp = slow.next; + slow.next = null; + return temp; + } + +} diff --git a/SortedLLToBST.java b/SortedLLToBST.java new file mode 100644 index 0000000..df8c040 --- /dev/null +++ b/SortedLLToBST.java @@ -0,0 +1,34 @@ +package LinkedList; + +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +import LinkedList.LinkedList.TreeNode; +public class SortedLLToBST { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(0); + ll.addLast(1); + ll.addLast(2); + ll.addLast(3); + ll.addLast(4); + ll.addLast(5); + ll.addLast(6); + ll.display(); + } + public static TreeNode convert(Node head){ + if(head==null||head.next==null) return new TreeNode(head.data); + Node slow = head; + Node fast = head; + Node tail = slow; + while(fast!=null && fast.next!=null){ + fast = fast.next.next; + tail = slow; + slow = slow.next; + } + TreeNode root = new TreeNode(slow.data); + tail.next = null;//previous of mid + root.left = convert(head); + root.right = convert(slow.next); + return root; + } +} diff --git a/SplitACircularLinkedListInto2Halves.java b/SplitACircularLinkedListInto2Halves.java new file mode 100644 index 0000000..072f8da --- /dev/null +++ b/SplitACircularLinkedListInto2Halves.java @@ -0,0 +1,30 @@ +package LinkedList; + +import LinkedList.LinkedList.Node; +public class SplitACircularLinkedListInto2Halves { + public static void main(String[] args) { + + } + static Node n1 = null; + static Node n2 = null; + static void split(Node node){ + if(node==null) return; + Node fast = node; + Node slow = node; + while(fast.next!=null && fast.next.next!=null){ + slow = slow.next; + fast = fast.next.next; + } + // fast.next.next = null -> even length LL + // fast.next = null -> odd length LL + if(fast.next.next != null){ + fast = fast.next;// end of LL + } + n1 = node; + if(node.next!=node){ + n2 = slow.next;//next of mid + } + slow.next = n1; + fast.next = n2; + } +} diff --git a/SplitLinkedListAlternaltely.java b/SplitLinkedListAlternaltely.java new file mode 100644 index 0000000..5f6b461 --- /dev/null +++ b/SplitLinkedListAlternaltely.java @@ -0,0 +1,51 @@ +package LinkedList; + +import LinkedList.LinkedList.LinkedList; +import LinkedList.LinkedList.Node; +public class SplitLinkedListAlternaltely { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(0); + ll.addLast(1); + ll.addLast(0); + ll.addLast(1); + ll.addLast(0); + ll.addLast(1); + ll.addLast(0); + ll.addLast(1); + split(ll.head); + display(n1); + display(n2); + } + static void display(Node node){ + while(node!=null){ + System.out.print(node.data+" "); + node = node.next; + } + System.out.println(); + } + static Node n1 = null; + static Node n2 = null; + static void split(Node node){ + if(node==null||node.next==null) return; + int level = 0; + n1 = node; + n2 = node.next; + Node t1 = n1; + Node t2 = n2; + while(node!=null){ + Node next = node.next; + node.next=null; + if(level%2==0){ + t1.next = node; + t1 = t1.next; + }else{ + t2.next = node; + t2 = t2.next; + } + node = next; + level++; + } + + } +} diff --git a/SplitLinkedListIntoParts.java b/SplitLinkedListIntoParts.java new file mode 100644 index 0000000..9a2b926 --- /dev/null +++ b/SplitLinkedListIntoParts.java @@ -0,0 +1,57 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +public class SplitLinkedListIntoParts { + public Node[] splitListToParts(Node head, int k) { + Node[] ans = new Node[k]; + int size = size(head); + if(k>=size){ + int n=0; + while(head!=null){ + Node temp = head.next; + head.next= null; + ans[n] = (head); + head = temp; + n++; + } + while(n0){ + tsize += 1; + rem--; + } + System.out.println(tsize); + + while(j 2nd ll + LinkedList l1 = new LinkedList(); + LinkedList l2 = new LinkedList(); + l1.addLast(1); + l1.addLast(2); + l1.addLast(3); + l1.addLast(4); + l1.addLast(5); + l1.addLast(6); + l1.addLast(7); + l2.addLast(7); + l2.addLast(8); + l2.addLast(9); + l1.head = subtract(l1.head, l2.head); + display(l1.head); + } + static void display(Node node){ + while(node!=null){ + System.out.print(node.data+" "); + node = node.next; + } + System.out.println(); + } + static Node subtract(Node n1, Node n2){ + n1 = reverse(n1); + n2 = reverse(n2); + int s1 = getSize(n1); + int s2 = getSize(n2); + if(s1>s2){ + add0(n2,s1-s2); + }else{ + add0(n1,s2-s1); + } + display(n1); + display(n2); + int carry = 0; + Node temp = n1; + Node tail = n1; + while(n1!=null){ + if(n1.data-n2.data<0){ + n1.data =((n1.data+10+carry)-n2.data); + carry=-1; + }else{ + n1.data = (n1.data+carry-n2.data); + carry = 0; + } + if(n1.next==null){ + tail = n1; + } + n1 = n1.next; + n2 = n2.next; + } + if(carry==-1){ + tail.data = tail.data*-1; + } + return reverse(temp); + + + } + static void add0(Node n, int s){ + Node temp = n; + while(temp.next!=null){ + temp = temp.next; + } + while(s>0){ + temp.next = new Node(0); + temp = temp.next; + s--; + } + } + static int getSize(Node n1){ + int ans =0 ; + while(n1!=null){ + n1 = n1.next; + ans++; + } + return ans; + } + static Node reverse(Node n1){ + if(n1==null||n1.next==null) return n1; + Node node = reverse(n1.next); + n1.next.next = n1; + n1.next=null; + return node; + } +} diff --git a/SwapNodesOfLL.java b/SwapNodesOfLL.java new file mode 100644 index 0000000..c328735 --- /dev/null +++ b/SwapNodesOfLL.java @@ -0,0 +1,66 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +public class SwapNodesOfLL { + public Node swapNodes(Node head, int k) { + if(head==null|| head.next==null) return head; + int size = size(head); + if(k>size/2){ + k = size-k+1; + } + if(k==1){ + Node prev2 = kth(head,k); + Node curr2 = prev2.next; + prev2.next = head; + curr2.next = head.next; + head.next=null; + head = curr2; + return head; + + } + Node prev2 = kth(head,k); + Node prev1 = head; + while(k>2){ + prev1 = prev1.next; + k--; + } + Node curr1 = prev1.next; + Node curr2 = prev2.next; + Node next1 = curr1.next; + Node next2 = curr2.next; + // System.out.println(curr1.data+" "+curr2.data); + if(curr1!=prev2){ + prev1.next = curr2; + curr2.next = next1; + prev2.next = curr1; + curr1.next = next2; + }else{ + prev1.next = curr2; + curr1.next = next2; + curr2.next = curr1; + } + return head; + } + public int size(Node node){ + int ans = 0; + while(node!=null){ + ans++; + node = node.next; + } + return ans; + } + public Node kth(Node node, int k){ + Node slow = node; + Node fast = node; + Node tail = slow; + while(k>1){ + fast = fast.next; + k--; + } + while(fast.next!=null){ + tail = slow; + slow = slow.next; + fast = fast.next; + } + return tail; + } +} diff --git a/TreeNode.java b/TreeNode.java new file mode 100644 index 0000000..30c7656 --- /dev/null +++ b/TreeNode.java @@ -0,0 +1,11 @@ +package LinkedList.LinkedList; + +public class TreeNode { + public int val; + public TreeNode left; + public TreeNode right; + public TreeNode(int val){ + this.val = val; + left = right=null; + } +} diff --git a/Unfold.java b/Unfold.java new file mode 100644 index 0000000..d69482e --- /dev/null +++ b/Unfold.java @@ -0,0 +1,50 @@ +package LinkedList; +import LinkedList.LinkedList.Node; +import LinkedList.LinkedList.LinkedList; +public class Unfold { + public static void main(String[] args) { + LinkedList ll = new LinkedList(); + ll.addLast(1); + ll.addLast(5); + ll.addLast(2); + ll.addLast(3); + ll.addLast(4); + ll.display(); + unfold(ll); + display(ll.head); + } + static void unfold(LinkedList ll){ + unfoldHelp(ll.head); + } + static void unfoldHelp(Node node){ + if(node==null|| node.next==null){ + return; + //nothing to unfold in a list of size 0 or 1 + } + Node fh = node;//first head + Node fp = fh;// first previous + + Node sh = node.next;// second head + Node sp = sh;//second previous + + while(sp!=null && sp.next!=null){ + // backup + Node f = sp.next; + // links + fp.next = f; + sp.next = f.next; + // move + fp = fp.next; + sp = sp.next; + } + fp.next = null; + sh = Reverse.reverseRecuriveHelp(sh); + fp.next = sh; + } + static void display(Node node){ + while(node!=null){ + System.out.print(node.data+" "); + node=node.next; + } + } +}