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.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void reorderList(ListNode* head) {
12         //分段
13         ListNode *p1,*p2;
14         p1=head;
15         p2=head;
16         if(!p2)
17             return;
18         p2=p2->next;
19
20         if(!p2)
21             return;
22
23         while(p1&&p1->next&&p2&&p2->next)
24         {
25             p1=p1->next;
26             p2=p2->next;
27             if(p2)
28                 p2=p2->next;
29         }
30         if(p1)
31         {
32             p2=p1->next;
33             p1->next=NULL;
34         }
35         p1=head;
36
37         if(p1==p2)
38             return;
39
40         //反转
41         ListNode *tnode=p2;
42         p2=p2->next;
43         tnode->next=NULL;
44
45         while(p2)
46         {
47             ListNode *temp=p2;
48             p2=p2->next;
49             temp->next=tnode;
50             tnode=temp;
51         }
52
53         //连接
54         ListNode *t=tnode;
55
56         while(p1)
57         {
58
59             if(t)
60             {
61                 ListNode *p12=p1;
62                 p1=p1->next;
63                 ListNode *t2=t;
64                 t=t->next;
65                 t2->next=p12->next;
66                 p12->next=t2;
67             }
68             else
69                 break;
70         }
71     }
72 };

note:反转时,42要放在43前面;

时间: 2024-10-20 00:27:05

leetcode143 Reorder List的相关文章

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, 重新排