就是有上下左右四个方向的子node,每个子node也可以有上下左右四个方向的子node,要求把这玩意儿转换成正常的doubly linkedlist,O(1) space,所以不能recursive,因为有stack space。
public class MultiListToDoubleList {
static class MultiListNode {
int val;
MultiListNode pre;
MultiListNode next;
MultiListNode up;
MultiListNode down;
}
public static void convert(MultiListNode head) {
if (head == null) {
return;
}
MultiListNode tail = head;
while (tail.next != null) {
tail = tail.next;
}
MultiListNode cur = head;
while (cur != null) {
if (cur.up != null) {
tail.next = cur.up;
cur.up.pre = tail;
while (tail.next != null) {
tail = tail.next;
}
cur.up = null;
}
if (cur.down != null) {
tail.next = cur.down;
cur.down.pre = tail;
while (tail.next != null) {
tail = tail.next;
}
cur.down = null;
}
cur = cur.next;
}
}
}
public class MultiListToDoubleList {
static class MultiListNode {
int val;
MultiListNode pre;
MultiListNode next;
MultiListNode up;
MultiListNode down;
}
. 涓€浜�-涓夊垎-鍦帮紝鐙��鍙戝竷
public static void convert(MultiListNode head) {. visit 1point3acres.com for more.
if (head == null) {
return;
}
MultiListNode tail = head;
while (tail.next != null) {
tail = tail.next;
}
MultiListNode cur = head;
while (cur != null) {
if (cur.up != null) {. Waral 鍗氬�鏈夋洿澶氭枃绔�,
tail.next = cur.up;
cur.up.pre = tail;
while (tail.next != null) {
tail = tail.next;. 涓€浜�-涓夊垎-鍦帮紝鐙��鍙戝竷
}
cur.up = null;
}
. from: 1point3acres.com/bbs
if (cur.down != null) {
tail.next = cur.down;. 鍥磋�鎴戜滑@1point 3 acres
cur.down.pre = tail;
while (tail.next != null) {. 涓€浜�-涓夊垎-鍦帮紝鐙��鍙戝竷
tail = tail.next;
}
cur.down = null;
}
cur = cur.next;
}
}-google 1point3acres
}. 鐗涗汉浜戦泦,涓€浜╀笁鍒嗗湴