reverse list between m and n
来源:互联网 发布:兰州网络教育学生平台 编辑:程序博客网 时间:2024/06/05 18:03
题目描述
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given1->2->3->4->5->NULL, m = 2 and n = 4,
return1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
我的算法:
public class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { if(head==null||head.next==null||m==n)return head; ListNode dummy=new ListNode(0); dummy.next=head; ListNode pre=dummy,tail,tmp; while(m-1>0){ pre=pre.next; n--; m--; } tail=pre; while(n>0){ tail=tail.next; n--; } tmp=pre.next; pre.next=tail; pre=tmp; while(pre!=tail){ tmp=pre; pre=pre.next; tmp.next=tail.next; tail.next=tmp; } return dummy.next; }}
0 0
- reverse list between m and n
- set all bits between i and j in N equal to M
- [CrackCode] 5.1 Set all bits between i and j in N equal to M
- difference between slist and list.
- Performance between Sets and List
- Difference Between List and Tuples
- Reverse linked list(逆转从n到m处的单链表)--leetcode
- Difference between proxy server and reverse proxy server
- what's the different between forward proxy and reverse proxy?
- Is there any difference between List x; and List x();?
- What is the difference between '/n' and '/r/n'? /n /r/n的区别是什么
- Difference between MPDU, MSDU, AMPDU, and AMSDU in 802.11n
- Difference between MPDU, MSDU, AMPDU, and AMSDU in 802.11n
- Difference between List and Set in Java Collection
- Difference between List and Set in Java Collection
- Difference between Set, List and Map in Java - Interview question
- differences between List and Set interface in point format
- Difference between List and Set in Java Collection
- 用python写一个简单的爬虫保存在json文件中
- eclipse 比较两个文本文件的 内容
- ArrayList和LinkedList add和remove方法的比较
- 【学习笔记】机器学习-K均值(k-means)
- Java之对象集合
- reverse list between m and n
- Calendar.HOUR_OF_DAY 和 Calendar.HOUR的区别
- Linux运维笔记-课后作业-软件安装
- Insertion Sort List and Binary Tree Paths
- unit11 网络管理
- Android---WebView内存泄漏解决方法
- Java多态
- Factorial Trailing Zeroes
- C#调用C++的dll文件方法