-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26c2f7b
commit f87c5b4
Showing
15 changed files
with
545 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,16 @@ | ||
package advance.crackingCodingInterview;public class ListNode { | ||
package advance.crackingCodingInterview; | ||
|
||
public class ListNode { | ||
|
||
public int data; | ||
public ListNode next; | ||
|
||
public ListNode(int data, ListNode next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
|
||
public ListNode(int data) { | ||
this.data = data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,32 @@ | ||
package advance.crackingCodingInterview;public class Tests { | ||
package advance.crackingCodingInterview; | ||
|
||
import advance.crackingCodingInterview.linkedList.general.DeleteDuplicates; | ||
import advance.crackingCodingInterview.linkedList.general.DeleteMiddle; | ||
import advance.crackingCodingInterview.linkedList.general.KthToLast; | ||
import advance.crackingCodingInterview.linkedList.general.PartitionAroundX; | ||
import advance.crackingCodingInterview.linkedList.palindromes.PalindromeStack; | ||
|
||
public class Tests { | ||
|
||
|
||
public static void main(String[] args) { | ||
// Create a linked list: 1 -> 3 -> 5 -> 7 -> 9 -> 11 -> 13 | ||
ListNode head = new ListNode(1); | ||
PartitionAroundX kthToLast = new PartitionAroundX(); | ||
kthToLast.add(3); | ||
kthToLast.add(5); | ||
kthToLast.add(8); | ||
kthToLast.add(5); | ||
kthToLast.add(10); | ||
kthToLast.add(2); | ||
kthToLast.add(1); | ||
|
||
// Test for 2nd to last | ||
System.out.println(kthToLast.partition(head, 5)); | ||
kthToLast.print(); | ||
|
||
// Test for 4th to last | ||
// ListNode result2 = kthToLast.kthToLast(head, 6); | ||
// System.out.println("6th to last: " + result2.data); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/advance/crackingCodingInterview/linkedList/general/DeleteDuplicates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 24 additions & 1 deletion
25
src/advance/crackingCodingInterview/linkedList/general/DeleteMiddle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
package advance.crackingCodingInterview.linkedList.general;public class DeleteMiddle { | ||
package advance.crackingCodingInterview.linkedList.general; | ||
|
||
import advance.crackingCodingInterview.ListNode; | ||
import advance.linkedlist.LeetCodingLinkedList; | ||
|
||
import java.util.List; | ||
|
||
public class DeleteMiddle { | ||
|
||
public ListNode deleteMiddle(ListNode head){ | ||
if(head == null || head.next == null) return null; | ||
|
||
ListNode slow = head; | ||
ListNode fast = head.next.next; | ||
|
||
|
||
while (fast != null && fast.next != null){ | ||
fast = fast.next.next; | ||
slow= slow.next; | ||
} | ||
slow.next = slow.next.next; | ||
return head; | ||
} | ||
|
||
} |
33 changes: 32 additions & 1 deletion
33
src/advance/crackingCodingInterview/linkedList/general/DetectLoopInLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
package advance.crackingCodingInterview.linkedList.general;public class DetectLoopInLinkedList { | ||
package advance.crackingCodingInterview.linkedList.general; | ||
|
||
import advance.crackingCodingInterview.ListNode; | ||
|
||
// | ||
public class DetectLoopInLinkedList { | ||
|
||
|
||
public ListNode findLoopInLinkedList(ListNode head){ | ||
ListNode fast = head; | ||
ListNode slow = head; | ||
|
||
while (fast != null && fast.next != null){ | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
|
||
if (slow == fast) break; | ||
} | ||
|
||
// Check if there is no meeting point, and therefore there will be no loop | ||
if (fast == null || fast.next == null) return null; | ||
|
||
// Move the slow to the head and keep the fast at the meeting point | ||
// If they keep going, they'll meet at the loop start. | ||
slow = head; | ||
while (slow != fast){ | ||
slow = slow.next; | ||
fast = fast.next; | ||
} | ||
return fast; | ||
} | ||
|
||
} |
33 changes: 32 additions & 1 deletion
33
src/advance/crackingCodingInterview/linkedList/general/KthToLast.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
package advance.crackingCodingInterview.linkedList.general;public class KthToLast { | ||
package advance.crackingCodingInterview.linkedList.general; | ||
|
||
import advance.crackingCodingInterview.ListNode; | ||
|
||
/** | ||
* Implement an algorithm to find the kth to last element of a singly linked list. | ||
*/ | ||
public class KthToLast { | ||
|
||
|
||
// TC O(n), SC O(1) | ||
public ListNode kthToLast(ListNode head, int kth){ | ||
if(head == null || kth < 1) return null; | ||
|
||
ListNode fast = head; | ||
ListNode slow = head; | ||
|
||
for (int i=0; i < kth; i++){ | ||
if (fast == null) return null; | ||
|
||
fast = fast.next; | ||
} | ||
|
||
// Move both pointers until the fast hit the last element at which time | ||
// the slow will hit the second to last element. | ||
|
||
while (fast != null){ | ||
slow = slow.next; | ||
fast = fast.next; | ||
} | ||
return slow; | ||
} | ||
} |
72 changes: 71 additions & 1 deletion
72
src/advance/crackingCodingInterview/linkedList/general/PartitionAroundX.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,72 @@ | ||
package advance.crackingCodingInterview.linkedList.general;public class PartitionAroundX { | ||
package advance.crackingCodingInterview.linkedList.general; | ||
|
||
import advance.crackingCodingInterview.ListNode; | ||
|
||
/** | ||
* Write a code to partition a linked list around a value x, such that | ||
* all nodes less than x comes before all nodes greater than or equal to x. If x is contained within the list, | ||
* the vales of x only need to be after the elements less than x. The partition element x can appear anywhere in the "right | ||
* partition"; it doesn't need to appear btw the left and right partitions. | ||
*/ | ||
public class PartitionAroundX { | ||
ListNode head; | ||
|
||
public ListNode partition(ListNode node, int X) { | ||
ListNode head = node; | ||
ListNode tail = node; | ||
|
||
while (node != null){ | ||
ListNode nextNode = node.next; | ||
|
||
if(node.data < X){ | ||
// Insert it at the head | ||
node.next = head; | ||
head = node; | ||
}else { | ||
node.next = tail; | ||
tail = node; | ||
} | ||
|
||
node = nextNode; | ||
} | ||
return head; | ||
} | ||
|
||
|
||
public ListNode partition2(ListNode node, int X) { | ||
ListNode beforeHead = new ListNode(0); // Head of the list with nodes < X | ||
ListNode before = beforeHead; | ||
ListNode afterHead = new ListNode(0); // Head of the list with nodes >= X | ||
ListNode after = afterHead; | ||
|
||
while (node != null) { | ||
if (node.data < X) { | ||
before.next = node; | ||
before = before.next; | ||
} else { | ||
after.next = node; | ||
after = after.next; | ||
} | ||
node = node.next; | ||
} | ||
|
||
after.next = null; // Set the end of the after list | ||
before.next = afterHead.next; // Join the two lists | ||
|
||
return beforeHead.next; // Return the head of the partitioned list | ||
} | ||
public void print() { | ||
ListNode temp = head; | ||
while (temp != null) { | ||
System.out.print(temp.data + " -> "); | ||
temp = temp.next; | ||
} | ||
System.out.println("END"); | ||
} | ||
|
||
public void add(int data) { | ||
ListNode node = new ListNode(data); | ||
node.next = head; | ||
head = node; | ||
} | ||
} |
46 changes: 36 additions & 10 deletions
46
src/advance/crackingCodingInterview/linkedList/palindromes/PalindromeRC.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 53 additions & 1 deletion
54
src/advance/crackingCodingInterview/linkedList/palindromes/PalindromeStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,54 @@ | ||
package advance.crackingCodingInterview.linkedList.palindromes;public class PalindromeStack { | ||
package advance.crackingCodingInterview.linkedList.palindromes; | ||
|
||
import advance.crackingCodingInterview.ListNode; | ||
|
||
import java.util.Stack; | ||
|
||
public class PalindromeStack { | ||
|
||
public ListNode head; | ||
|
||
public boolean isPalindrome(ListNode head){ | ||
ListNode fast = head; | ||
ListNode slow = head; | ||
|
||
Stack<Integer> container= new Stack<>(); | ||
|
||
while (fast != null && fast.next != null){ | ||
container.push(slow.data); | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
// Has Odd number of elements | ||
if (fast != null){ | ||
slow = slow.next; | ||
} | ||
|
||
while (slow != null){ | ||
int top = container.pop(); | ||
|
||
// If values are different, it is not a palindrome | ||
if (top != slow.data){ | ||
return false; | ||
} | ||
slow = slow.next; | ||
} | ||
return true; | ||
} | ||
|
||
public void add(int data) { | ||
ListNode node = new ListNode(data); | ||
node.next = head; | ||
head = node; | ||
} | ||
|
||
public void print() { | ||
ListNode temp = head; | ||
while (temp != null) { | ||
System.out.print(temp.data + " -> "); | ||
temp = temp.next; | ||
} | ||
System.out.println("END"); | ||
} | ||
} |
Oops, something went wrong.