Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for linked list reversal problem. #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions linked_list_cycle/solutions/cycleInLinkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct sll_int_node
{
int iData;
struct sll_int_node *pNextNode;
} sll_int_node;

/* Allocates a node and sets its parameters */
void createNewIntNode_Sll(sll_int_node **ppNewNode, int iData, sll_int_node *pNextNode)
{
*ppNewNode = (sll_int_node *)malloc(sizeof(sll_int_node));
(*ppNewNode)->iData = iData;
(*ppNewNode)->pNextNode = pNextNode;
}

void printList_Sll(sll_int_node *pHead)
{
while(pHead != NULL)
{
printf("%d ", pHead->iData);
pHead = pHead->pNextNode;
}
}

/* Insert to the start of the list */
void insertFrontIntNode_Sll(sll_int_node **ppHead, int iNewData)
{
sll_int_node *pNewNode = NULL;

createNewIntNode_Sll(&pNewNode, iNewData, *ppHead);

*ppHead = pNewNode;
}

bool detectLoop_Sll(sll_int_node *pHead)
{
sll_int_node *pSlow = pHead;
sll_int_node *pFast = pHead;

if(pHead != NULL)
{
while((pFast->pNextNode != NULL) && (pFast->pNextNode->pNextNode != NULL))
{
pSlow = pSlow->pNextNode;
pFast = pFast->pNextNode->pNextNode;

if(pSlow == pFast)
return true;
}
}
else
printf("ERR(detectLoop): List is empty\n");

return false;
}

int main(void)
{
sll_int_node *head = NULL;

insertFrontIntNode_Sll(&head, 1);
insertFrontIntNode_Sll(&head, 2);
insertFrontIntNode_Sll(&head, 3);
insertFrontIntNode_Sll(&head, 4);
insertFrontIntNode_Sll(&head, 5);

/* Before inserting loop */
printf("List contains loop: %s\n", detectLoop_Sll(head) ? "true" : "false");

head->pNextNode->pNextNode->pNextNode->pNextNode->pNextNode = head->pNextNode->pNextNode;

/* After inserting loop */
printf("List contains loop: %s\n", detectLoop_Sll(head) ? "true" : "false");

return 0;
}
119 changes: 119 additions & 0 deletions linked_list_reverse/solutions/reverseLinkedList.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int iData;
struct sll_int_node *pNextNode;
} sll_int_node;

void reverseList_Iter_Sll(sll_int_node **ppHead)
{
sll_int_node *pPrev = NULL;
sll_int_node *pCurr = *ppHead;
sll_int_node *pNext = NULL;

if(*ppHead != NULL)
{
while(pCurr != NULL)
{
pNext = pCurr->pNextNode;
pCurr->pNextNode = pPrev;

pPrev = pCurr;
pCurr = pNext;
}

*ppHead = pPrev;
}
else
printf("ERR(reverseList_Iter_Sll): List is empty\n");
}

sll_int_node * reverseSubList_Sll(sll_int_node *pPrev, sll_int_node *pCurr)
{
sll_int_node *pNewHead;

if(pCurr->pNextNode == NULL)
{
pCurr->pNextNode = pPrev;
return pCurr;
}

pNewHead = reverseSubList_Sll(pCurr, pCurr->pNextNode);
pCurr->pNextNode = pPrev;

return pNewHead;
}

void reverseList_Recur_Sll(sll_int_node **ppHead)
{
if(*ppHead != NULL)
{
*ppHead = reverseSubList_Sll(NULL, *ppHead);
}
else
printf("ERR(reverseList_Recur_Sll): List is empty\n");
}

/* Allocates a node and sets its parameters */
void createNewIntNode_Sll(sll_int_node **ppNewNode, int iData, sll_int_node *pNextNode)
{
*ppNewNode = (sll_int_node *)malloc(sizeof(sll_int_node));
(*ppNewNode)->iData = iData;
(*ppNewNode)->pNextNode = pNextNode;
}

void printList_Sll(sll_int_node *pHead)
{
while(pHead != NULL)
{
printf("%d ", pHead->iData);
pHead = pHead->pNextNode;
}
}

/* Insert to the start of the list */
void insertFrontIntNode_Sll(sll_int_node **ppHead, int iNewData)
{
sll_int_node *pNewNode = NULL;

createNewIntNode_Sll(&pNewNode, iNewData, *ppHead);

*ppHead = pNewNode;
}

int main(void)
{
sll_int_node *head1 = NULL, *head2 = NULL;

insertFrontIntNode_Sll(&head1, 1);
insertFrontIntNode_Sll(&head1, 2);
insertFrontIntNode_Sll(&head1, 3);
insertFrontIntNode_Sll(&head1, 4);

insertFrontIntNode_Sll(&head2, -1);
insertFrontIntNode_Sll(&head2, -2);
insertFrontIntNode_Sll(&head2, -3);
insertFrontIntNode_Sll(&head2, -4);

printf("List1: ");
printList_Sll(head1);
printf("\n");

reverseList_Iter_Sll(&head1);
printf("Reversed List1 (Iterative): ");
printList_Sll(head1);
printf("\n");

printf("List2: ");
printList_Sll(head2);
printf("\n");

reverseList_Recur_Sll(&head2);
printf("Reversed List2 (Iterative): ");
printList_Sll(head2);
printf("\n");

return 0;
}
125 changes: 125 additions & 0 deletions minimum_stack/solutions/minStack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct stacknode_int
{
int iData;
struct stacknode_int *pNextNode;
} stacknode_int;

typedef struct
{
stacknode_int *pOrigStackTop;
stacknode_int *pMinStackTop;
} minStackNode_int;

/* Allocates a node and sets its parameters */
static void createNewStackNode_Int(stacknode_int **ppNewNode, int iData, stacknode_int *pNextNode)
{
*ppNewNode = (stacknode_int *)malloc(sizeof(stacknode_int));
(*ppNewNode)->iData = iData;
(*ppNewNode)->pNextNode = pNextNode;
}

bool isStackEmpty(stacknode_int *pTop)
{
return (pTop == NULL);
}

bool peekFromStack(stacknode_int *pTop, int *ipData)
{
if(isStackEmpty(pTop))
{
printf("ERR(Trying to peek): Stack Empty!\n");
return false;
}

*ipData = pTop->iData;

return true;
}

void printStack(stacknode_int *pTop)
{
while(pTop != NULL)
{
printf("%d ", pTop->iData);
pTop = pTop->pNextNode;
}
}

void pushToMinStack(minStackNode_int *pStack, int iNewData)
{
stacknode_int *pNewNode = NULL;
int iMinStackData = 0;

createNewStackNode_Int(&pNewNode, iNewData, (pStack->pOrigStackTop));
pStack->pOrigStackTop = pNewNode;

if(!isStackEmpty(pStack->pMinStackTop) && (iNewData > (pStack->pMinStackTop->iData)))
iMinStackData = pStack->pMinStackTop->iData;
else
iMinStackData = iNewData;

createNewStackNode_Int(&pNewNode, iMinStackData, (pStack->pMinStackTop));
pStack->pMinStackTop = pNewNode;
}

bool popFromMinStack(minStackNode_int *pStack, int *ipPoppedData)
{
stacknode_int *pOldTop = NULL;

if(isStackEmpty(pStack->pOrigStackTop))
{
printf("ERR(Trying to pop): Stack Underflow!\n");
return false;
}

pOldTop = pStack->pMinStackTop;
pStack->pMinStackTop = pOldTop->pNextNode;
free(pOldTop);

pOldTop = pStack->pOrigStackTop;
*ipPoppedData = pOldTop->iData;
pStack->pOrigStackTop = pOldTop->pNextNode;
free(pOldTop);

return true;
}

bool getMinFromMinStack(minStackNode_int *pStack, int *ipPeekedData)
{
return peekFromStack(pStack->pMinStackTop, ipPeekedData);
}

int main(void)
{
minStackNode_int s;
int iMin = 0, iDummy = 0;

s.pMinStackTop = NULL;
s.pOrigStackTop = NULL;

pushToMinStack(&s, 15);
pushToMinStack(&s, 20);
pushToMinStack(&s, 10);

getMinFromMinStack(&s, &iMin);
printf("Stack content: ");
printStack(s.pOrigStackTop);
printf("\tMin: %d\n", iMin);

pushToMinStack(&s, 5);
getMinFromMinStack(&s, &iMin);
printf("Stack content: ");
printStack(s.pOrigStackTop);
printf("\tMin: %d\n", iMin);

popFromMinStack(&s, &iDummy);
popFromMinStack(&s, &iDummy);
getMinFromMinStack(&s, &iMin);
printf("Stack content: ");
printStack(s.pOrigStackTop);
printf("\t\tMin: %d\n", iMin);
}
33 changes: 33 additions & 0 deletions target_sum/solutions/TargetSumPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.*;
import java.lang.*;
import java.io.*;

class TargetSumPair
{
public static boolean checkForSumPair(int[] iArr, int iSum)
{
if(iArr.length >= 2)
{
Set<Integer> set = new HashSet<Integer>();

set.add(iArr[0]);
for(int i = 1; i < iArr.length; i++)
{
if(set.contains(iSum - iArr[i]))
return true;

set.add(iArr[i]);
}
}
return false;
}

public static void main (String[] args) throws java.lang.Exception
{
int[] iArrTest1 = {5, 3, 4, 2, 8, -1, 15};
int[] iArrTest2 = {3, 4, 5};

System.out.println("Test1-> " + checkForSumPair(iArrTest1, 13));
System.out.println("Test2-> " + checkForSumPair(iArrTest2, 13));
}
}