leetcode143- Reorder List问题

题目要求:

Given a singly linked list LL0→L1→…→Ln-1→Ln,
    reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes‘ values.

思路:

初看到这个题目,只是提出了空间复杂度的要求,并没有提出时间复杂度要求,因此我开始的思路比较简单。

首先固定一个位置指针,指示每次要插入的位置;

查找链表的最后一个结点;

将链表的最后一个结点插入到指示位置;

位置指针下移一步。

结束条件是:进行了链表长度的1/2次

该种思路运行结果正确,但是超时了。。。。

好坑啊,你也没要提示我时间复杂度啊

思路2:

考虑到时间复杂度的问题,又采取了另外一种思路。

首先使用快慢指针,找到链表的中心;

将链表的后半部分进行逆序;

逆序完成后,将后半部分依次插入到指定位置。

该种方法时间复杂度为O(n),空间复杂度为O(1)

代码:

public static void reorderList(ListNode head){
if(head == null || head.next == null || head.next.next == null){
return;
}else{
ListNode ptr = head, curptr = null, fast = null, slow = null;
int len = 0; //记录链表长度
while(ptr != null){
len++;
ptr = ptr.next;
}

//slow指针走到链表中心
slow = head;
fast = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}

//查找链表中心的前一个元素
ListNode ptr1 = head, ptr2 = slow;
while(ptr1.next != slow){
ptr1 = ptr1.next;
}
//后半部分链表逆序
if(len%2 == 0){
//偶数个元素
fast = slow.next;
for(int i = 0; i < len/2-1; i++){
ptr = fast.next;
fast.next = slow;
slow = fast;
fast = ptr;
}
ptr1.next = null;
ptr2.next = null;

//偶数个元素的链表逆序完成后依次插入
curptr = head;
ListNode ptr3 = null;
for(int i=0; i<len/2; i++){
ptr = curptr.next;
curptr.next = slow;
ptr3 = slow.next;
slow.next = ptr;
curptr = ptr;
slow = ptr3;
}
}else{
//奇数个元素
slow = slow.next;
ptr1 = slow;
fast = slow.next;
for(int i= 0; i < len/2-1;i++){
ptr = fast.next;
fast.next = slow;
slow = fast;
fast = ptr;
}
ptr2.next = null;
ptr1.next = null;

//奇数个元素逆序后依次插入元素
curptr = head;
ListNode ptr3 = null;
for(int i = 0; i < len/2; i++){
ptr = curptr.next;
curptr.next = slow;
ptr3 = slow.next;
slow.next = ptr;
curptr = ptr;
slow = ptr3;
}
}

}
}

时间: 2024-08-19 09:40:27

leetcode143- Reorder List问题的相关文章

leetcode143 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}. 1 /** 2 * Definition for singly-linked list. 3 *

leetcode-143. Reorder List

刚开始刷题,一开始没思路,上网看了一下别人的思路才写出来 总的思路就是先把链表分为两部分,可以先遍历链表再根据长度分也可以用快慢指针(新知识点),然后将第二部分反转,再依次插入到第一部分.思路很简单,但没用ide还是出错了... /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }

[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}. 这道题是将一个给定的单链表按照某种规则进行重排序,要求是不可以简单地直接交换结点中的值. 思路

143. 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}. 题解: 这道题虽然看上去很复杂,但经过分析,可知实质上这道题就是

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}. 这是一道比较综合的链表题目.一开始拿到手足无措.慢慢分析了一下,其实做法无非分三步: 1.将链表分

[Linked List]Reorder List

otal Accepted: 54991 Total Submissions: 252600 Difficulty: Medium 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

Reorder the Books HDU 5500

Reorder the Books Accepts: 127 Submissions: 237 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 dxy家收藏了一套书,这套书叫<SDOI故事集>,<SDOI故事集>有n(n\leq 19)n(n≤19)本,每本书有一个编号,从11号到nn号. dxy把这些书按编号从小到大,从上往下摞成一摞.dxy对这套书极其

]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}. 题目的意思就是,给定一个链表,从两头开始链接, 比如1-2-3-4-5-6,最开始取两头,组成1-

leetcode 【 Reorder List 】python 实现

题目: 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}. 代码: oj 测试通过 248 ms 1 # Definition for singly-

Reorder List leetcod

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}. 题目的意思是要求对一个链表进行重新排序,如上面所示将 L0→L1→-→Ln-1→Ln, 重新排