Algorithms—143.Reorder List

来源:互联网 发布:软件测试前景是什么 编辑:程序博客网 时间:2024/04/29 05:47

思路:老套路,把ListNode的每个节点写进List,然后拼接

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public void reorderList(ListNode head) {if (head!=null) {List<ListNode> list = zuhe(head, new ArrayList<ListNode>());int l=list.size();for (int i = 0; i < l/2; i++) {list.get(i).next = list.get(l - 1 - i);list.get(l - 1 - i).next = list.get(i + 1);}list.get(l/2).next = null;}}public List<ListNode> zuhe(ListNode head, List<ListNode> list) {while (head!=null) {list.add(head);head=head.next;}return list;}}


耗时:392ms,上游


0 0
原创粉丝点击