- 浏览: 174671 次
- 性别:
- 来自: 济南
文章分类
最新评论
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}.
给定一个链表让我们重新排列它,例如以前的顺序为0-1-2-3-4,排列后的顺序为0-4-1-3-2。我们首先要找到中间的节点,用快慢指针,这个比较简单,不过值得注意的是节点是奇数和偶数的不同情况。找到中间节点后我们可以借助堆栈将后半部分的节点压栈,然后从头结点开始一个一个的处理,可以解决。堆栈实际上帮我们将链表的后半段反转了,如果我们不借助堆栈也可以解决,我们可以将链表从中间节点分为两段,将后半段反转,然后再将两个链表合为一个链表,这样就不需要借助堆栈,时间复杂度为O(n), 空间复杂度为O(1)。下面是两种方法的代码:
1,Using stack
2,Without stack
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}.
给定一个链表让我们重新排列它,例如以前的顺序为0-1-2-3-4,排列后的顺序为0-4-1-3-2。我们首先要找到中间的节点,用快慢指针,这个比较简单,不过值得注意的是节点是奇数和偶数的不同情况。找到中间节点后我们可以借助堆栈将后半部分的节点压栈,然后从头结点开始一个一个的处理,可以解决。堆栈实际上帮我们将链表的后半段反转了,如果我们不借助堆栈也可以解决,我们可以将链表从中间节点分为两段,将后半段反转,然后再将两个链表合为一个链表,这样就不需要借助堆栈,时间复杂度为O(n), 空间复杂度为O(1)。下面是两种方法的代码:
1,Using stack
/** * 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) { Stack<ListNode> stack = new Stack<ListNode>(); if(head == null) return; ListNode fast = head; ListNode slow = head; while(fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } if(fast != null){ slow = slow.next; } while(slow != null) { stack.push(slow); slow = slow.next; } while(!stack.isEmpty()){ ListNode top = stack.pop(); ListNode temp = head.next; head.next = top; top.next = temp; head = temp; } head.next = null; } }
2,Without stack
/** * 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 || head.next == null) return; ListNode fast = head; ListNode slow = head; ListNode pre = null; //get the middle node, and cut list into two sublists while(fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; } ListNode midNode = slow.next; slow.next = null; // reverse the latter part ListNode newNode = reverseLatterPart(midNode); //merge two sublists while(newNode != null) { ListNode tem = head.next; head.next = newNode; head = newNode; newNode = tem; } } private ListNode reverseLatterPart(ListNode head) { ListNode prev = null; ListNode tem = null; while(head != null) { tem = head.next; head.next = prev; prev = head; head = tem; } return prev; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 229Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 232You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 348Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 342Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 461Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 525Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 435Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 624Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 435The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 392Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 522Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 542Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 378All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 869Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 884Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 559Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 606Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 771Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 741You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 635For a undirected graph with tre ...
相关推荐
介绍了Ajax控件-ReorderList的使用方法:添加数据,修改数据,删除数据,以及拖动排序。除了修改数据有部分代码,其他功能皆无代码(ReorderList的界面拖动排序也无需代码),都是一次性绑定数据源控件。~~还在为网络...
ASP.NET AJAX的ReorderList控件是可以实现无排序数据绑定的列表控件,从名字上就可以看出来因为它是Reorder的,也就是说能够通过和服务器端交互数据重新排序绑定的数据项。要实现重排序,用户只需简单的拖动某条记录的...
语言:English (United States) 重新排序列表/库列 它是针对SharePoint用户和开发人员的工具。 该工具将有助于对列表或库列进行重新排序。 首先,登录到SharePoint...https://github.com/dipongkor/reorder-rolumns.git
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary Longest Palindromic Substring Regular Expression Matching Wildcard Matching...
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
利用ReorderList控件实现拖拽排序;利用Rating控件实现评分功能;利用Accordion控件实现QQ样式的菜单。 第6章 注册登录。 第8章 留言本。 第9章 分页模块。 第10章 文件上传显示进度条功能。 第11章 相册模块。 第12...
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
首先介绍了ASP.NET AJAX基础知识和结构,然后介绍了ASP.NET AJAX Control Toolkit中的全部控件,如AutoComplete、PasswordStrength、CollapsiblePanel、Tabs、CascadingDropDown、ReorderList、SlideShow等,...
Given a singly linked list$ L: L_0→L_1→…→L{n-1}→L_n$, reorder it to: $L_0→L_n →L_1→L_{n-1}→L_2→L_{n-2}→…$ You must do this in-place without altering the nodes' values. For example, Given...
31 ReorderList 任意添加列表内容并更改列表顺序 32 ResizableControlExtender 控件大小改变 33 RoundedCornersExtender 圆角大小 34 SliderExtender 类似音量大小那种拖动条空间 SlideShowExtender 幻灯片一张...
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
第1章 Ajax简介以及使用的场合 第2章 浏览器中的JavaScript ... 第3章 XMLHttpRequest对象 3.1.3 最简单的Ajax示例 ...Calender ReorderList控件的使用 RatingExample Rating控件的属性或方法
Calender ReorderList控件的使用 RatingExample Rating控件的属性或方法 第15章(\C06) MyLogin ASP.NET Ajax注册登录 第16章(\C07) AddressList Ajax通讯录 第17章(\C08) LinkageList 级联...
Calender ReorderList控件的使用 RatingExample Rating控件的属性或方法 第15章(\C06) MyLogin ASP.NET Ajax注册登录 第16章(\C07) AddressList Ajax通讯录 第17章(\C08) LinkageList 级联菜单(使用VS ...
ASP.net Ajax Control Toolkit控件应用: 包括:利用AutoCompleteExtender控件实现自动完成的功能;...利用ReorderList控件实现拖拽排序;利用Rating控件实现评分功能;利用Accordion控件实现QQ样式的菜单。
这是我面试的一家公司的实实在在的项目。版本1.3变化: 新增了将问题添加到类别的功能新增了从类别中删除问题的功能类别现在显示标题中的问题数量版本1.2 版本1.1 版本1.0 yarn start 在开发模式下运行应用程序。...