思路:老套路,把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,上游
本文探讨了在链表操作中的一种优化方法,通过将链表节点逐个添加到列表中,并利用列表进行节点间的高效拼接,从而改进了数据处理效率。详细阐述了算法实现步骤和性能评估,提供了实际应用案例。
1313

被折叠的 条评论
为什么被折叠?



