leetcode每日一题——反转链表
public static ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
for(int i = 1; i < m; i++){
pre = pre.next;
}
head = pre.next;
for(int i = m; i < n; i++){
ListNode nex = head.next;
head.next = nex.next;
nex.next = pre.next;
pre.next = nex;
}
return dummy.next;
}
public static ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
for(int i = 1; i < m; i++){
pre = pre.next;
}
head = pre.next;
for(int i = m; i < n; i++){
ListNode nex = head.next;
head.next = nex.next;
nex.next = pre.next;
pre.next = nex;
}
return dummy.next;
}
全部评论
相关推荐
09-09 23:10
泰山学院 C++ 点赞 评论 收藏
分享
09-09 13:56
门头沟学院 硬件开发 点赞 评论 收藏
分享
09-01 18:13
门头沟学院 后端工程师 点赞 评论 收藏
分享
09-10 18:19
门头沟学院 Java 点赞 评论 收藏
分享