java - LinkedList function from InterviewBit -
i'm struggling solution for question on interviewbit.
i linked full description, in short:
1) given head node of linkedlist
2) take first half of list , change values that:
"1st node’s new value = last node’s value - first node’s current value 2nd node’s new value = second last node’s value - 2nd node’s current value"
here approach (it compiles not mutate list @ all)
i see method not modify original list -- seems i'm doing making new list correctly altered values, not changing original.
/** * definition singly-linked list. * class listnode { * public int val; * public listnode next; * listnode(int x) { val = x; next = null; } * } */ public class solution { public listnode subtract(listnode a) { listnode current = a; int length = 0; //get length while(current.next != null){ length++; current = current.next; } length += 1; while(current.next != null){ double half = math.floor(length/2); for(int i=0; i<half; i++ ){ // // if(i == 0){ // int aval = (nthtolast(a, length)).val - a.val; // a.val = ((nthtolast(a, length-i)).val - a.val); // a.next = current; // } current.val = ((nthtolast(a, length-i)).val - current.val); current = current.next; } } return a; } /* helper function given linkedlist head, , int n, returns nth last listnode in linkedlist */ public listnode nthtolast(listnode head, int n){ listnode nth = head; listnode ahead = head; /* strategy: set nth head, , 'ahead' n places in front of 'nth' increment @ same speed , when 'ahead' reaches end, 'nth' in nth place end. */ while(ahead.next != null){ for(int i=0; i<n; i++){ ahead = ahead.next; } nth = nth.next; ahead = ahead.next; } return nth; } }
also -- i'm trying better @ questions these. ok approach question? i'd figure out how make work, if around bad approach please let me know.
break code simple helper functions, make function value of nth element in linked list(this function easy write) ,and traverse list upto half every time calling function value of listsize-i member of list , edit value of the ith member of list. , make changes 1st few elements manually check weather linklist implementation working or not
/** * definition singly-linked list. **/ class listnode { public int val; public listnode next; listnode(int x) { val = x; next = null; } } public class solution { public listnode subtract(listnode a) { listnode current = a; int length = 0; //get length while(current.next != null){ length++; current = current.next; } length += 1; int half = length/2; //logic of loop //go 0 half of list // j goes last element half //for example if size of list 6 (indexing 0) //when 0 j 5 //when 1 j 4 , on //so wanted for(int i=0,j=length-1; i<half; i++,j-- ){ current.val=nthelement(a,j).val-current.val; current = current.next; } return a; } /* helper function given linkedlist head, , int n, returns nth node of linkedlist */ public listnode nthelement(listnode head, int n){ //e.g-if n 5 return 5th node listnode nth = head; for(int i=0; i<n; i++){ nth = nth.next; } return nth; } }
Comments
Post a Comment