-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from fengyuanyang/issue-132
issue-132: separate Linked List with partition value
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ordestiny/tdd/interview/linkedlist/LinkedListNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ordestiny.tdd.interview.linkedlist; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public class LinkedListNode<E> { | ||
LinkedListNode<E> next; | ||
private E data; | ||
|
||
public boolean hasNext() { | ||
return next != null; | ||
} | ||
|
||
public E getData() { | ||
return data; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/ordestiny/tdd/interview/linkedlist/LinkedListNodeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.ordestiny.tdd.interview.linkedlist; | ||
|
||
public class LinkedListNodeUtil { | ||
|
||
/** | ||
* interview Question: | ||
* A LinkedListNode, with N elements, | ||
* each element has its value(integer) | ||
* | ||
* given a partition value x, all the values in LinkedListNode must be separated into two parts, | ||
* values below x in left part, values above x must be in right part, then concatenate them | ||
* ex, | ||
* Given | ||
* When LinkedListNode 3-> 5->8 ->5->10->2->1 , and partition = 5 | ||
* Expect 3-> 2->1 ->5->8->5->10 | ||
* | ||
* @param node | ||
* @param x | ||
* @return | ||
*/ | ||
public static LinkedListNode partition(LinkedListNode node , int x) { | ||
if (node == null || node.next == null) | ||
return node; | ||
LinkedListNode leftCurrentNode = null; | ||
LinkedListNode leftNodeHead = null; | ||
LinkedListNode rightCurrentNode = null; | ||
LinkedListNode rightNodeHead = null; | ||
LinkedListNode currentNode = null; | ||
do { | ||
if (currentNode == null) | ||
currentNode = node; | ||
else | ||
currentNode = currentNode.next; | ||
int nodeValue = (int) currentNode.getData(); | ||
LinkedListNode newNode = LinkedListNode.builder().data(nodeValue).build(); | ||
if ( nodeValue < x) { | ||
if (leftNodeHead == null) { | ||
leftNodeHead = newNode; | ||
leftCurrentNode = leftNodeHead; | ||
} else { | ||
leftCurrentNode.next = newNode; | ||
leftCurrentNode = leftCurrentNode.next; | ||
} | ||
|
||
} else { | ||
if (rightNodeHead == null) { | ||
rightNodeHead = newNode; | ||
rightCurrentNode = rightNodeHead; | ||
} else { | ||
rightCurrentNode.next = newNode; | ||
rightCurrentNode = rightCurrentNode.next; | ||
} | ||
} | ||
} while(currentNode.hasNext()); | ||
|
||
if (leftNodeHead == null) | ||
return rightNodeHead; | ||
if (rightNodeHead == null) | ||
return leftNodeHead; | ||
leftCurrentNode.next = rightNodeHead; | ||
|
||
return leftNodeHead; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/com/ordestiny/tdd/interview/linkedlist/LinkedListNodeUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.ordestiny.tdd.interview.linkedlist; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public class LinkedListNodeUtilTest { | ||
LinkedListNodeUtil util = new LinkedListNodeUtil(); | ||
|
||
@ParameterizedTest | ||
@MethodSource("partitionArguments") | ||
void partition(LinkedListNode<Integer> node, int x, LinkedListNode<Integer> expectedNode) { | ||
LinkedListNode<Integer> currentNode = util.partition(node, x); | ||
|
||
do { | ||
Assertions.assertEquals(expectedNode.getData(), currentNode.getData()); | ||
if (currentNode.hasNext()) { | ||
currentNode = currentNode.next; | ||
expectedNode = expectedNode.next; | ||
} | ||
|
||
} while (currentNode.hasNext()); | ||
} | ||
|
||
@Test | ||
void partition_withNullNode_expectNull() { | ||
LinkedListNode<Integer> currentNode = util.partition(null, 5); | ||
|
||
Assertions.assertEquals(null, currentNode); | ||
} | ||
|
||
private static Stream<Arguments> partitionArguments() { | ||
return Stream.of( | ||
Arguments.of(generate(3, 5, 8, 5, 10, 2, 1), 5, generate(3, 2, 1, 5, 8, 5, 10)), | ||
Arguments.of(generate(3, 11, 20, 7, 9, 10), 9, generate(3, 7, 11, 20, 9, 10)), | ||
Arguments.of(generate(20, 19, 23, 13, 6, 5, 0), 0, generate(20, 19, 23, 13, 6, 5, 0)), | ||
Arguments.of(generate(20, 19, 23, 13, 6, 5, 0), 25, generate(20, 19, 23, 13, 6, 5, 0)), | ||
Arguments.of(generate(20), 22, generate(20)) | ||
); | ||
} | ||
|
||
private static LinkedListNode generate(int... nodeValues) { | ||
LinkedListNode node = null; | ||
for (int i= nodeValues.length-1;i>=0;i--) { | ||
if (node == null) | ||
node = LinkedListNode.builder().data(nodeValues[i]).build(); | ||
else | ||
node = LinkedListNode.builder().data(nodeValues[i]).next(node).build(); | ||
} | ||
return node; | ||
} | ||
|
||
} |