小刀初试 [LeetCode] Reorder List, Solution Given a singly linked listL:L0→L1→…→Ln-1→Ln, reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given{1,2,3,4}, reorder it to{1,4,2,3}. [Thoughts] 目前...
public class Solution { public void reorderList(ListNode head) { if(head!=null&&head.next!=null){ ListNode low=head;//1. split such list into two list, first and second, according to slow and fast point ListNode fast=head; while(fast.next!=null&&fast.next.next!=null){ low=low.next;...
[LeetCode]Reorder List 题目描述:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4,2,3}....
cur->next =recurse(next);returnhead; }voidreorderList(ListNode *head) {if(head == NULL || head->next ==NULL)return; head=recurse(head); } }; 第二种解法,比较有效率,O(N)的时间复杂度,O(1)空间复杂度。思路是:将链表平均分为前后2段(l和r),将r倒转,然后按照顺序将l和r拼接起来。AC了 ...
*/publicclassSolution{publicvoidreorderList(ListNodehead){if(head!=null&&head.next!=null){ListNodelow=head;//1. split such list into two list, first and second, according to slow and fast pointListNodefast=head;while(fast.next!=null&&fast.next.next!=null){low=low.next;fast=fast.next.next...
1publicclassSolution {2publicvoidreorderList(ListNode head) {3//IMPORTANT: Please reset any member data you declared, as4//the same Solution instance will be reused for each test case.5if(head ==null){6return;7}89ListNode fast = head, slow =head;10while(fast.next !=null&& fast.next.ne...
Solution { 10 public: 11 void reorderList(ListNode *head) { 12 // IMPORTANT: Please reset any member data you declared, as 13 // the same Solution instance will be reused for each test case.14 if (head ==NULL || head->next == NULL){ 15...
classSolution:#@param head, a ListNode#@return nothingdefreorderList(self, head):ifheadisNoneorhead.nextisNoneorhead.next.nextisNone:returnhead node=head.nextwhilenode.next.nextisnotNone: node=node.next new_head=head.next end=node.next ...
/** * Source : https://oj.leetcode.com/problems/reorder-list/ * * Given a singly linked list L: L0→L1→…→Ln-1→Ln, * reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… * * You must do this in-place without altering the nodes' values. * * For example, * Given {1,2,...
https://oj.leetcode.com/problems/reorder-list/ 思路:分三步走 从中间把链表分开(双指针法) 把后面的链表反序 合并两个链表 publicclassSolution {publicvoidreorderList(ListNode head) {if(head ==null|| head.next ==null|| head.next.next ==null)return; ...