【LeetCode】Reorder List

方法1:利用map<curNode,preNode> 获取节点的上一个节点为O(l).

 

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
	        if(head == null || head.next == null) return ;
	        Map<ListNode, ListNode>map = new HashMap<>();
	        ListNode dummy = new ListNode(-1);
	        dummy.next = head;
	        ListNode pre = null,end = null;
	        while(head != null){
	            map.put(head ,pre);
	            pre = head;
	            head = head.next;
	        }
	        end = pre;
	        head = dummy.next;
	        ListNode newHead = new ListNode(-1);
	        boolean flag = false;
	        while(head != null && end != null && head != end){
	            newHead.next = head;
	            newHead = newHead.next;
	            //System.out.print(newHead.val + " ");
	            head = head.next;
	            newHead.next = end;
	            newHead = newHead.next;
	            //System.out.print(newHead.val + " ");
	            if(head == end){
	            	//
	                flag = true;
	                break;
	            }

	            end = map.get(end);
	        }
	        if(!flag){
	            newHead.next = head;
	            newHead = newHead.next;
	            //System.out.print(newHead.val + " *");
	            newHead.next = null;
	        }
	        else{
	            newHead.next = null;
	        }
	    }

}


方法2:获取后半段链表,然后反转,最后与前半段合并

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null)return head;
        ListNode pre = null, cur = head, next = head.next;
        while(next != null){
            cur.next = pre;
            pre = cur;
            cur = next;
            next = next.next; 
        }
        cur.next = pre;
        return cur;
    }
    public ListNode getMiddleNode(ListNode head){//获取中间节点,保证前半段长度大于等于后半段长度
        ListNode dummy = head;
        ListNode fast = dummy, low = dummy, pre = null;
        while(fast.next != null && fast.next.next != null){
            low = low.next;
            fast = fast.next.next;
        }
        ListNode middle = low.next;
        low.next = null;
        return middle;
    }
    public ListNode mergeTwoList(ListNode h1, ListNode h2){
        ListNode dummy = new ListNode(-1);
        ListNode p = dummy;
        while(h2 != null  ){
            p.next = h1;
            p = p.next;
            h1 = h1.next;//需要先指过来,要不然得不到h1.next的信息
            p.next = h2;
            p = p.next;
            h2 = h2.next;
        }
        p.next = h1;
        return dummy.next;
    }
    public void reorderList(ListNode head) {
	        if(head == null || head.next == null) return ;
	        ListNode middle = getMiddleNode(head);
	        mergeTwoList(head, reverseList(middle));
	    }
	    /**
	     * 1 2 3 4 最先开始 slow指向2,fast指向3
	     * /

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值