设为首页 加入收藏

TOP

leetcode_92_Reverse Linked List II
2015-07-20 17:20:43 来源: 作者: 【 】 浏览:3
Tags:leetcode_92_Reverse Linked List

描述:

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.

思路:

这种题目,举个例子能让思路更加清晰,通过在草纸上演算可知,题目要分两种情况,m==1和m>1的情况,然后就是围绕这两种情况展开讨论,删除后面的结点,然后将后面的结点添加到前面,一次搞定,bravo!

代码:

public ListNode reverseBetween(ListNode head, int m, int n) {
		if (head==null) {
			return null;
		}
        ListNode p =head,q=head,temp=null;
        int i=0;
        for(i=0;i
  
   

结果:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇LeetCode[Sort]: Maximum Gap 下一篇C++]LeetCode: 133 Largest Recta..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)