Middle-题目77:92. Reverse Linked List II

原创 2016年05月31日 16:56:28

题目原文:
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.
题目大意:
给出一个单链表和两个数m,n,翻转链表的m~n位。
题目分析:
还是用two pointers的思想,记住m的前驱,令其后继指向n,而n的后继指向m的next,以此类推。(在纸上画画,并多考虑一些特殊情况,如m是开头,n是结尾等)
源码:(language:c)

struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
     if (m == n || !head || !head->next)
         return head;
     int i = 1;
     struct ListNode* mPtr = head, *nPtr = head;
     for (struct ListNode* node = head; node; node = node->next, i++) {
         if (i == m)
             mPtr = node;
         if (i == n)
             nPtr = node;
     }
     if (mPtr == head) {
         while (mPtr != nPtr) {
             struct ListNode* temp = mPtr;
             mPtr = mPtr->next;
             temp->next = nPtr->next;
             nPtr->next = temp;
         }
         return nPtr;
     }
     else {
         struct ListNode* mPtrPrev = head;
         while (mPtrPrev->next != mPtr)
             mPtrPrev = mPtrPrev->next;
         mPtrPrev->next = nPtr;
         while (mPtr != nPtr) {
             struct ListNode* temp = mPtr;
             mPtr = mPtr->next;
             temp->next = nPtr->next;
             nPtr->next = temp;
         }
         return head;
     }
 }

成绩:
0ms,100%
Cmershen的碎碎念:
这道题在纯链表节点处理的题里面算是较难的了,须结合源码并在纸上演示一下才能理解指针间的关系。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

( Leetcode 92 ) Reverse Linked List II

题目:Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one...

LeetCode 92. Reverse Linked List II

1. 题目描述 Reverse a linked list from position m to n. Do it in-place and in one-pass. For examp...

leetcode No92. Reverse Linked List II

Question: Reverse a linked list from position m to n. Do it in-place and in one-pass. For exam...

LeetCode 92. Reverse Linked List II ***** 虚拟头结点

题目 题意 注意 思路 代码题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. ...

Leetcode 92. Reverse Linked List II

92. Reverse Linked List II    My Submissions Question Editorial Solution Total Accepted: 6...

【一天一道LeetCode】#92. Reverse Linked List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

92. Reverse Linked List II 等题

206. Reverse Linked List原题: Reverse a singly linked list.题解: 用递归很容易解决这个问题,几个问题,要先获取最后一个元素(未来的首元素),...
  • Mr_Zing
  • Mr_Zing
  • 2016年11月20日 22:30
  • 160

【LEETCODE】92-Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1...

LeetCode --- 92. Reverse Linked List II

题目链接:Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-p...
  • makuiyu
  • makuiyu
  • 2015年04月09日 10:50
  • 444

[Leetcode]#92 Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1->2-...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目77:92. Reverse Linked List II
举报原因:
原因补充:

(最多只允许输入30个字)