-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
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,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 + "]"; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |