LeetCode-- Reverse Linked List II
2015-12-02 10:31
381 查看
题目描述:
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
思路:
1. 使用栈来保存m到n之间的数字,其余元素使用队列保存
2. 在[m,n]区间外时,循环弹出栈内元素到链表
3. 在[m,n]区间内,先循环弹出队列元素到链表,再创建栈,最后需要判断当前head是否为空
实现代码:
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
思路:
1. 使用栈来保存m到n之间的数字,其余元素使用队列保存
2. 在[m,n]区间外时,循环弹出栈内元素到链表
3. 在[m,n]区间内,先循环弹出队列元素到链表,再创建栈,最后需要判断当前head是否为空
实现代码:
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode ReverseBetween(ListNode head, int m, int n) { var stack = new Stack<int>(); var q = new Queue<int>(); ListNode node = null; var c = 1; ListNode newNode = null; while(head != null) { if(c >= m && c <=n){ while(q.Count > 0){ var first = MoveNext(ref node , q.Dequeue()); if(first){ newNode = node; } } while(c >= m && c<= n){ stack.Push(head.val); head = head.next; c++; } if(head == null){ while(stack.Count > 0){ var first = MoveNext(ref node , stack.Pop()); if(first){ newNode = node; } } } } else{ while(stack.Count > 0){ var first = MoveNext(ref node , stack.Pop()); if(first){ newNode = node; } } var f = MoveNext(ref node , head.val); if(f) { newNode = node; } head = head.next; c++; } } return newNode; } private bool MoveNext(ref ListNode n , int val){ if(n == null){ n = new ListNode(val); return true; } else{ n.next = new ListNode(val); n = n.next; return false; } } }
相关文章推荐
- 【DP】hdu3507
- 【Information:java: javacTask: 源发行版 1.7 需要目标发行版 1.7】
- svn代码回滚命令
- Linux系统启动流程详解
- Zookeeper超级用户
- 我的大学——学习生活总结
- 每个人都应该知道的14个电脑使用技巧
- Git 的 .gitignore 配置
- SQL基础---增删查询操作
- autoload-cache-2.4 发布
- Spark使用经验分享
- 如何用VisualStudio2013、VisualStudioTools命令行 编写C语言程序
- JAVA AQS源码分析
- 找出文件中的完全平方数
- compass模块----Helpers
- OpenCv ROI操作
- HTTP请求中浏览器的缓存机制
- LeetCode-- Palindrome Number
- SQL----函数
- watchOS 2 教程(四): Watch Connectivity