leetcode || 143、Reorder List

problem:

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}.

Hide Tags

Linked List

题意:对单链表重新排序,依次将尾节点插入到头节点的后面。

thinking:

(1)由于单链表只有一个next指针,所以需要借助stack存储节点指针。

(2)每次将stack的顶部节点插入到单链表的头结点的后面,修改相关指针,头指针往后遍历

(3)直到头指针等于stack顶部元素(单链表节点总素为奇数)或者,头指针的下一个节点为stack的顶部元素(总素为偶数),停止遍历

,将最后一个节点next指针置空

code:时间复杂度O(n)

class Solution {
  public:
      void reorderList(ListNode* head) {
          stack<ListNode *> _stack;
          ListNode *tmp=head;
          while(tmp!=NULL)
          {
              _stack.push(tmp);
              tmp=tmp->next;
          }
          if(_stack.size()<3)
              return;
          ListNode *p= head;
          ListNode *q=_stack.top();
          while(p!=q && p->next!=q)
          {
              _stack.pop();
              ListNode *p_next=p->next;
              p->next=q;
              q->next=p_next;
              p=p_next;
              q=_stack.top();
          }
          q->next=NULL;
      }
  };
时间: 2024-08-02 12:06:01

leetcode || 143、Reorder List的相关文章

LeetCode解题报告:Reorder List

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.针对第二个子链表求倒序

leetcode || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

leetcode || 119、Pascal&#39;s Triangle II

problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

leetcode 143. Reorder List ----- java

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.使用list记录链表. /** * Definition for si

Java for LeetCode 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}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

[leetcode]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 may not modify the values in the list's nodes, only nodes itself may be changed. Example 1: Given 1->2->3->4, reorder it to 1->4->2->3. Example 2: G

LeetCode 143题:Reorder List的分析及两种解题思路

特别说明:参考了很多前辈的文章,整理如下,我只做了重新编码的工作,不能保证代码最优,主要用作交流学习. 题目: 方法1:将链表的每个节点地址保存在指针数组中,利用数组随机访问调整链表. 1 struct ListNode 2 { 3 int val; 4 ListNode *next; 5 ListNode(int x) : val(x), next(NULL) {} 6 }; 7 8 void reorderList(ListNode *head) 9 { 10 if (head == nul

LeetCode 136、137、260(只出现一次的数,异或性质及应用)

First. 陈列一下“异或”的一些性质 异或是一种基于二进制的位运算,用符号XOR或者 ^ 表示,其运算法则是对运算符两侧数的每一个二进制位,同值取0,异值取1. 它与布尔运算的区别在于,当运算符两侧均为1时,布尔运算的结果为1,异或运算的结果为0. 性质 1.交换律 2.结合律(即(a^b)^c == a^(b^c)) 3.对于任何数x,都有x^x=0,x^0=x 4.自反性 A ^ B ^ B = A ^  0 = A  应用 一.交换两个整数的值而不必用第三个参数 a = 9; b =

LeetCode 13、罗马数字转整数

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做  XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 .同样地,数字