Skip to content

Commit

Permalink
Add some codes for linkedlist
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaryousmane committed Mar 19, 2024
1 parent 26c2f7b commit f87c5b4
Show file tree
Hide file tree
Showing 15 changed files with 545 additions and 124 deletions.
16 changes: 15 additions & 1 deletion src/advance/crackingCodingInterview/ListNode.java
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;
}
}
32 changes: 31 additions & 1 deletion src/advance/crackingCodingInterview/Tests.java
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package advance.crackingCodingInterview.linkedList.palindromes;
package advance.crackingCodingInterview.linkedList.general;

import advance.crackingCodingInterview.ListNode;

Expand Down
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;
}

}
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;
}

}
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;
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
package advance.crackingCodingInterview;
// Check if the linked list is a palindrome
package advance.crackingCodingInterview.linkedList.palindromes;
// Check if the linked list is a palindrome using the reverse and compare approach

public class Palindrome {
import advance.crackingCodingInterview.ListNode;

public class PalindromeRC {

public ListNode head;

public boolean isPalindrome(ListNode head) {
return false;
ListNode reversed = reverseAndClone(head);
return isEqual(head, reversed);
}

static boolean isEqual(ListNode firstNode, ListNode secondNode){
while (firstNode != null && secondNode != null){
if (firstNode.data != secondNode.data){
static boolean isEqual(ListNode firstNode, ListNode secondNode) {
while (firstNode != null && secondNode != null) {
if (firstNode.data != secondNode.data) {
return false;
}
firstNode = firstNode.next;
secondNode = secondNode.next;
secondNode = secondNode.next;
}
return firstNode == null && secondNode == null;
}

public ListNode reverseAndClone(ListNode node){

public ListNode reverseAndClone(ListNode node) {
this.head = null;
while (node != null) {
ListNode newNode = new ListNode(node.data);
newNode.next = head;
head = newNode;
node = node.next;
}
return head;
}

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");
}


Expand Down
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");
}
}
Loading

0 comments on commit f87c5b4

Please sign in to comment.