zoukankan      html  css  js  c++  java
  • flatten 4-direction doubly linked list.

    就是有上下左右四个方向的子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
            
    }. 鐗涗汉浜戦泦,涓€浜╀笁鍒嗗湴
    

      

  • 相关阅读:
    Java设计模式(学习整理)---工厂模式
    Java Swing 使用总结(转载)
    Java-生成验证码图片(自定义内容,尺寸,路径)
    二维码(带有图片)的生成
    J2se中的声音---AudioPlayer
    文件的读取和写入(指定路径)
    ASP.NET:使用Flurl制作可复用的分页组件
    ASP.NET:Forms身份验证和基于Role的权限验证
    ASP.NET:MVC模板化机制
    ASP.NET:MVC中文件上传与地址变化处理
  • 原文地址:https://www.cnblogs.com/apanda009/p/7927134.html
Copyright © 2011-2022 走看看