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->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ? m ? n ? length of list.

思路

这里需要操作连续的三个节点,分别是 pre  cur 和 nxt ,->口->口->口

这里 用到了4个指针,revtail 和 revhead  表示翻转后的子链表的头和尾(即m节点和n节点),newhead 和 newtail 表示翻转的起始节点和结束节点(即m-1节点和n+1节点); 

代码一:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!head || m>=n)
            return head;
        ListNode *pre = head;
        ListNode *cur = head;
        ListNode *nxt = cur->next;
        ListNode *revtail = NULL;
        ListNode *revhead = NULL;
        ListNode *newtail = NULL;
        ListNode *newhead = NULL;        
        int i = 1;
        while(cur)
        {
          nxt = cur->next; 
          if(i==m)
          {
              revtail = cur;  
              newhead = pre;
              pre->next = NULL;
          }
          if(i>m && i<n)
          {
              cur->next = pre;
          }
          if(i==n)
          {
              cur->next = pre;
              revhead = cur;
              newtail = nxt;
          }
          i++;
          pre = cur;
          cur = nxt;
        }
        if(newhead!=revtail)
        {
            newhead->next = revhead;
            revtail->next = newtail;
            return head;
        }
        else
        {
            revtail->next = newtail;
            return revhead;
        }
        
    }
};


代码二:这里我申请了一个Dump节点作为附加的头结点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!head || m>=n)
            return head;
        ListNode dump(0);
        dump.next = head;
        ListNode *pre = &dump;
        ListNode *cur = head;
        ListNode *nxt = cur->next;
        ListNode *revtail = NULL;
        ListNode *revhead = NULL;
        ListNode *newtail = NULL;
        ListNode *newhead = NULL;        
        int i = 1;
        while(cur && i<=n)
        {
          nxt = cur->next; 
          if(i==m)
          {
              revtail = cur;  
              newhead = pre;
          }
          if(i>m && i<n)
          {
              cur->next = pre;
          }
          if(i==n)
          {
              cur->next = pre;
              revhead = cur;
              newtail = nxt;
          }
          i++;
          pre = cur;
          cur = nxt;
        }
        newhead->next = revhead;
        revtail->next = newtail;
        return dump.next;        
    }
};


 代码三:这里减少重复的指针,尽量少的利用指针变量

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseBetween(ListNode *head, int m, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!head || m>=n)
            return head;
        ListNode dump(0);
        dump.next = head;
        ListNode *pre = &dump;
        ListNode *cur = head;
        ListNode *nxt = NULL;
        for(int i=1;i<m;i++)
        {
            pre = cur;
            cur = cur->next;
        }
        ListNode *newhead = pre;
        ListNode *newtail = cur;
        pre = cur;
        cur = cur->next;      
        int j = m+1;
        while(cur && j++<=n)
        {
            nxt = cur->next;
            cur->next = pre;
            pre = cur;
            cur = nxt;
        }
        newtail->next = cur;  
        newhead->next = pre;        
        return dump.next;        
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值