Complexity: O(N)
Program:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package careercup.google; /** * * @author Harit */ public class DoublyLinkedListNode { private int value; private DoublyLinkedListNode previous; private DoublyLinkedListNode next; public DoublyLinkedListNode(int v){ this.value = v; this.previous = null; this.next = null; } public void setPrevious(DoublyLinkedListNode prev){ this.previous = prev; } public void setNext(DoublyLinkedListNode n){ this.next = n; } public int getValue(){ return this.value; } public DoublyLinkedListNode getNext(){ return this.next; } public DoublyLinkedListNode getPrevious(){ return this.previous; } }* To change this template, choose Tools | Templates * and open the template in the editor. */ package careercup.google; /** * * @author Harit */ public class ReverseDLinkedList { private static DoublyLinkedListNode tail = null; private static DoublyLinkedListNode head = null; public static void main(String args[]){ for(int i=0; i < 10; i++){ DoublyLinkedListNode node = new DoublyLinkedListNode(i); if(head == null){ head = node; tail = node; } if(i != 0){ tail.setNext(node); node.setPrevious(tail); tail = node; } } printList(head); System.out.println(""); System.out.println("Reversing the list"); printList(reverseList(head)); } private static DoublyLinkedListNode reverseList(DoublyLinkedListNode head){ DoublyLinkedListNode start = head; DoublyLinkedListNode temp = null; while(head != null){ temp = head.getNext(); head.setNext(head.getPrevious()); head.setPrevious(temp); if(head.getPrevious() == null){ head.setPrevious(start); break; } head = head.getPrevious(); } return head; } private static void printList(DoublyLinkedListNode head){ System.out.print("List : "); while(head != null){ System.out.print(head.getValue() + " "); head = head.getNext(); } } }Output:
run:List : 0 1 2 3 4 5 6 7 8 9Reversing the listList : 9 8 7 6 5 4 3 2 1 0BUILD SUCCESSFUL (total time: 0 seconds)
Java : reversing a doubly linkedlist on linear time
10 Sepimplementing malloc in C
12 Febimplement 2D array dynamically in C
20 Jan}
for(i = 0; i < n; i++){
free(a[i]);
}
free(a);
}
remove duplicates from list of integers – C
17 Janfor(i = 0; i < len; i ++ ){
printHash(&hashMap[i]);
}
printf(“\n”);
}
void printHash(struct node** elem){
while((*elem) != NULL){
printf(” %d “,(*elem)->data);
(*elem) = (*elem)->next;
}
}
int doHash(int num){
int a = 1;
int b = 2;
int n = 5;
int ret = 0;
/* we will use hash function as h(x) = (ax + b)mod n */
ret = (a * num + b) % n;
return ret;
}
void fixInHash(int value, struct node** elem) {
struct node* current = NULL;
current = (struct node*) malloc(sizeof(struct node));
int flag = 0;
//printf(“value : %d\n”,value);
if( *elem == NULL){
binary search – c
17 Janprintf(“mid – found at index : %d\n”,mid);
exit(0);
}
if(num < a[mid]){
//printf(“less: %d – %d\n”, start, (mid-1));
doBinarySearch(a, start, (mid -1), num);
}
if(num > a[mid]){
//printf(“greater : %d – %d\n”, (mid+1),len);
doBinarySearch(a, (mid + 1), len, num);
}
}
sort array of 0s and 1s
17 JanThis is O(N) solution
permute the string – C program
15 Janint permuteString(char []);void doPermute(char [], char [], int [], int , int );
main() {
char string[] = “sun”; int stat = permuteString(string);
}
int permuteString(char inString[]) {
int length,i, *used; char *out;
length = strlen(inString);
out = (char *)malloc(length + 1); if(!out) { return 0; }
/* so that printf doesn’t run past the end of buffer */ out[length] = ”; used = (int *)malloc(sizeof(int) * length);
if(!used) { return 0; }
/* start with no letters used, so zero array */ for( i = 0; i < length; i ++) { used[i] = 0; }
doPermute(inString, out, used, length, 0);
free(out);
free(used);
return 1; //success
}
void doPermute(char in[], char out[], int used[], int length, int recurLevel ) {
int i ;
/* base case */
if(recurLevel == length ) {
printf(“%s\n”, out);
return;
}
/* recursive case */
for(i = 0; i < length ; i ++) {
if(used[i]) {
continue;
}
out[recurLevel] = in[i]; /* put current letter in output */
used[i] = 1; /* mark letter as used */
doPermute(in, out, used, length, recurLevel + 1);
used[i] = 0; /* unmark this letter */
}
}
Build linkedlist at tail/end – C
14 JanThere are two approaches given in the solution – using addNode() and addNodeWithDummy()
Appending nodes in linkedlist – C program
14 JanInteger/String conversions in C
14 Jana.) Given an string you have to convert the string into integer. The following program does that for you
