Skip to content

Commit

Permalink
Add ListNode and LinkedListTest
Browse files Browse the repository at this point in the history
  • Loading branch information
bky373 committed Jun 12, 2024
1 parent 4228681 commit eaaa487
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/example/ListNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.example;

public class ListNode {

public int value;
public ListNode next;

public ListNode() {
}

public ListNode(int value) {
this.value = value;
}

public ListNode(int value, ListNode next) {
this.value = value;
this.next = next;
}

@Override
public String toString() {
return "ListNode[" + value + "]";
}

public String toDetailString() {
return "ListNode[v=" + value + ", next=" + next + "]";
}
}
63 changes: 63 additions & 0 deletions src/test/java/LinkedListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import static support.ListNodeSupport.NODE_1;
import static support.ListNodeSupport.NODE_2;
import static support.ListNodeSupport.NODE_3;
import static support.ListNodeSupport.NODE_4;

import java.util.ArrayList;
import java.util.List;
import org.example.ListNode;
import org.junit.jupiter.api.Test;
import support.ListNodeSupport;

public class LinkedListTest {

/**
* https://leetcode.com/problems/reorder-list/
*/
@Test
void reorderList() {
ListNode head = ListNodeSupport.linkNext(NODE_1, NODE_2, NODE_3, NODE_4);
System.out.println("head = " + head.toDetailString());

// 0,1,2,3
// 0,3 - left=0, right=3
// 0,3,1 - left=1, right=3
// 0,3,1,2 - left=1, right=2
// 0,3,1,2 [end] - left=2, right=2

// 0,1,2,3,4
// 0,5 - left=0, right=4
// 0,5,1 - left=1, right=4
// 0,5,1,4 - left=1, right=3
// 0,5,1,4,2 - left=2, right=3
// 0,5,1,4,2,3 - left=2, right=2
// 0,5,1,4,2,3 [end] - left=3, right=3

if (head.next == null) {
return;
}
List<ListNode> nodes = new ArrayList<>();
ListNode reorderHead = head;
ListNode reorderHead2 = head;
while (head != null) {
ListNode next = head.next;
head.next = null;
nodes.add(head);
head = next;
}

int left = 0;
int right = nodes.size() - 1;
while (left < right) {
reorderHead.next = nodes.get(right);
right--;
left++;
if (left <= right && left < nodes.size()) {
reorderHead = reorderHead.next;
reorderHead.next = nodes.get(left);
reorderHead = reorderHead.next;
}
}
System.out.println("reorderHead = " + reorderHead2);
}
}
33 changes: 33 additions & 0 deletions src/test/java/support/ListNodeSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package support;

import java.util.Arrays;
import java.util.List;
import org.example.ListNode;

public class ListNodeSupport {

public static final ListNode NODE_1 = new ListNode(1);
public static final ListNode NODE_2 = new ListNode(2);
public static final ListNode NODE_3 = new ListNode(3);
public static final ListNode NODE_4 = new ListNode(4);
public static final ListNode NODE_5 = new ListNode(5);
public static final ListNode NODE_6 = new ListNode(6);
public static final ListNode NODE_7 = new ListNode(7);
public static final ListNode NODE_8 = new ListNode(8);
public static final ListNode NODE_9 = new ListNode(9);
public static final ListNode NODE_10 = new ListNode(10);

public static ListNode from(int val) {
return new ListNode(val);
}

public static ListNode linkNext(ListNode... nodes) {
List<ListNode> list = Arrays.stream(nodes)
.toList();

for (int i = 0; i < list.size() - 1; i++) {
list.get(i).next = list.get(i + 1);
}
return list.get(0);
}
}

0 comments on commit eaaa487

Please sign in to comment.