`
blue2048
  • 浏览: 184729 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]Reorder List-java

阅读更多

注意以下几点

1. 传入为空

 

由于题目要求比较宽泛,所以采用以下两种方式解答

1. 用空间降低复杂度,使用list存储,借助数组编号寻址,重组链表

/**
 * 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){
            return;
        }
        List<ListNode> list = new ArrayList<ListNode>();
        while (head !=null ){
            list.add(head);
            head = head.next;
        }
        ListNode a = new ListNode(-1);
        for(int i=0; i<=(list.size()-1)/2; i++){
            int j = list.size() - 1- i;
            a.next = list.get(i);
            a = a.next;
            a.next = list.get(j);
            a = a.next;
            a.next=null;
        }
    }
}

 

2. 纯链表操作,链表分成前后两段,后段倒序,两段链表进行merge

public void reorderList(ListNode head){
        if(head == null){
            return;
        }
        ListNode t = new ListNode(-1);
        t.next = head;
        ListNode slow = t;
        ListNode fast = t;
        while (fast != null){
            slow = slow.next;
            fast = fast.next;
            if(fast !=null){
                fast = fast.next;
            }
        }
        ListNode tailHead = null;
        ListNode tailP1 = slow.next;
        if(tailP1 == null){
            return;
        }
        slow.next = null;
        if(tailP1.next != null){
            ListNode tailP2 = tailP1.next;
            tailP1.next=null;
            if(tailP2.next != null){
                ListNode tailP3 = tailP2.next;
                while (tailP3 != null){
                    tailP2.next = tailP1;
                    tailP2 = tailP3;
                    tailP1 = tailP2;
                    tailP3 = tailP3.next;
                }
                tailP2.next = tailP1;
            }else {
                tailP2.next = tailP1;
            }
            tailHead = tailP2;
        }else {
            tailHead = tailP1;
        };
        ListNode a = new ListNode(-1);
        while (head !=null && tailHead != null){
            a.next = head;
            head = head.next;
            a.next.next = tailHead;
            tailHead = tailHead.next;
            a=a.next.next;
        }
        if(head!=null){
            a.next = head;
        }
        if(tailHead != null){
            a.next = tailHead;
        }
        System.out.println();
    }

 

 

分享到:
评论

相关推荐

    leetcode-answer-and-analysis(part).zip_图形图像处理_Java_

    在给定的压缩包"leetcode-answer-and-analysis(part).zip"中,主要涵盖了与图形图像处理和Java编程语言相关的LeetCode算法题目解答及分析。以下是针对这些题目所涉及的知识点的详细说明: 1. **Reorder List** ...

    leetcode中325题python-leetcode:leetcode

    leetcode中325题python leetcode 以 参考 和 Hash相关 1_两数之和 387_字符串中的第一个唯一字符 链表操作 ...reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双指针遍历/滑动

    LeetCode:LeetCode解决方案

    LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...

    Leetcode题目+解析+思路+答案.pdf

    - **Reorder List**:按照特定规则重新排列链表。 - **Partition List**:将链表按值分割成两个部分。 - **Add Two Numbers**:两个非负整数相加,结果存储在链表中。 - **Copy List with Random Pointer**:...

    java-leetcode题解之第143题重排链表.zip

    public ListNode reorderList(ListNode head) { if (head == null || head.next == null) return head; // 分离奇数和偶数链表 ListNode odd = head, even = head.next, evenHead = even; while (even != null...

Global site tag (gtag.js) - Google Analytics