Geet
BAN USER1)Make for loop of tgt
2)if ith element of tgt is 0 then contine (skip current iteration) with loop
3)Now find the position of ith element of tgt in src array and store it in say p variable.
4)check if postion found in 3rd step is equal to i?
5)if position is not equal to i than store p found in 3rd step to other variable say p2.
6)do while loop till p2!=i
7)in this while loop check if p2 is equal to p. this will be true for first iteration of while loop
8) if p2==p then replace zero in src array with p th element in src, then store new postion of pth element in p2.
9)if condition checked in step 7 is not true then
10) initialize temp=0;
11) And replace zero in src array with temp th element in src
12) Now replace zero with p2 in src array.
13) after while loop increment temp;
Below is pseudocode
for(int i=0;i<tgt.length;i++){
if(tgt[i]==0){
continue;
}
int position=findPositionOfInSrc(tgt[i])
if(position!=i){
int p2=position;
while(p2!=i){
if(p2==position){
replaceWithZeroInSrc(position);
int p2=findPositionOfInSrc(tgt[i]);
}
else{
replaceWithZeroInSrc(temp);
replaceWithZeroInSrc(p2);
int p2=findPositionOfInSrc(tgt[i]);
}
}
temp++;
}
}
replaceWithZeroInSrc(int position){
int zeroPosition=findZeroPosition(src);
int g=src[position];
src[position]=src[zeroPosition];
src[zeroPosition]=g;
}
Another solution in Java. What you guys think about it.
public ListNode doStuff(ListNode head) {
ListNode prev = null;
ListNode start = null;
ListNode lastNowFirst = head;
while (lastNowFirst.next != null) {
lastNowFirst = lastNowFirst.next;
}
boolean flg = false;
while (head != null && head.next != null) {
ListNode last = head;
while (last.next != null) {
prev = last;
last = last.next;
}
prev.next = null;
last.next = head;
if (flg) {
start.next = last;
}
start = head;
head = head.next;
flg = true;
}
return lastNowFirst;
}
- Geet February 05, 2013Look at below method in java. This method accept 2 areguments, both are first node of two different list which we want to merge one by one. Any suggestion is appreciated.
public ListNode mergeOneByOne(ListNode node1, ListNode node2) {
ListNode node1Next = null, node2Next = null, head = null;
if (node1 != null && node2 != null) {
head = node1;
while (node1 != null && node2 != null) {
if (node1.next == null && node2.next == null) {
node1.next = node2;
break;
}
node1Next = node1.next;
node2Next = node2.next;
if (node1Next == null && node2Next != null) {
node1Next = node2Next;
node1.next = node2;
node2.next = node1Next;
break;
}
node1.next = node2;
node2.next = node1Next;
node1 = node1Next;
node2 = node2Next;
}
}
return head;
}
Below is Java code for it. any suggestion is appreciated.
public ListNode kReversemTravel(ListNode head, int k, int m) {
ListNode init = head;
int i = 0, j = 0, l = 0;
ListNode previous = null, nxt = null, curr = null, last = null;
while (head != null && head.next != null) {
curr = head;
last = previous;
while (head != null && i < k) {
nxt = head.next;
head.next = previous;
previous = head;
head = nxt;
i++;
}
l++;
i = 0;
curr.next = head;
if (l == 1) {
init = previous;
} else {
last.next = previous;
}
previous = head;
if (head != null) {
while (head.next != null && j < m) {
previous = head;
head = head.next;
j++;
}
j = 0;
}
}
return init;
}
- Geet February 03, 2013
Here only one change, this is DLL so first node have pointer of it's previous node which would be tail node so there will no need to traverse to end of the list to get last node. So now from first (head) node travel to next node of it and from tail travel to previous node of it, and when both head and tail node are same then stop.
- Geet August 16, 2013