链表的转置问题

**206. Reverse Linked List **
Reverse a singly linked list.
代码如下:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur!=NULL)
        {
            ListNode* pNext = cur->next;
            cur->next = pre;
            pre = cur;
            cur = pNext;
        }
        return pre;
    }
};

**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->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.
代码如下:

class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(m==n||head==NULL)
          return head;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* first1 = newhead;
        ListNode* last1 = newhead;
        while(m>1)
        {
            first1 = first1->next;
            m--;
        }
        ListNode* first2 = first1->next;
        while(n>0)
        {
            last1 = last1->next;
            n--;
        }
        ListNode* last2 = last1->next;
        reverse(first1,first2,last1,last2);
        return newhead->next;
    }
    void reverse(ListNode* first1,ListNode* first2,ListNode* last1,ListNode* last2)
    {
        ListNode* cur = first2;
        ListNode* pre = last2;

        while(cur!=last2)
        {
            cout<val<next;
            cur->next = pre;
            pre = cur;
            cur = pNext;
            
        }
        first1->next = last1;
        first2->next =last2;
        return;
    }
};

**61. Rotate List **
Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
代码如下:

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if(head==NULL)
           return NULL;
        int n = getLength(head);
        k = k%n;
        int t = n-k;
        if(k<0||k>n||n==0)
           return NULL;
        if(k==0)
           return head;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* p = head;
        ListNode* q = head;
        while(t>1)
        {
           p = p->next;
           t--;
        }
        while(n>1)
        {
           q = q->next;
           n--;
        }
        newhead->next = p->next;
        p->next = NULL;
        q->next = head;
        return newhead->next;
    }
    
    int getLength(ListNode* list)
    {
        int count = 0;
        while(list!=NULL)
        {
           list = list->next;
           count++;
        }
        return count;
    }
};

**24. Swap Nodes in Pairs **
Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
代码如下:

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head==NULL)
          return NULL;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* p = head;
        while(p!=NULL&&p->next!=NULL)
        {
            int temp = p->val;
            p->val = p->next->val;
            p->next->val = temp;
            
            p = p->next->next;;
        }
        return newhead->next;
    }
};

25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
代码如下:

class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if(head==NULL)
          return NULL;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        ListNode* p = newhead;
        ListNode* q = newhead;
        ListNode* first = head; 
        ListNode* second = head;
        int count = k;
        int n = getLength(head);
        while(count--)
        {   
            q = q->next;
            second = second->next;
            n--;
            if(second==NULL&&count!=0)
              return head;
        }
        
        while(second!=NULL||n==0)
        {   
            p->next = reverse(first,second);
            if(n==0)
              break;
            count = k;
            first = second;
            while(count--)
            {
                p = p->next;
                second = second->next;
                n--;
                if(second==NULL)
                  break;
            }
            if(second==NULL&&count!=0)
              break;
        }
        return newhead->next;
    }
    ListNode* reverse(ListNode* first,ListNode* second)
    {
        ListNode* cur = first;
        ListNode* pre = second;
        while(cur!=second)
        {
            ListNode* pNext = cur->next;
            cur->next = pre;
            pre = cur;
            cur = pNext;
        }
        return pre;
    }
    int getLength(ListNode* list)
    {
        int num = 0;
        while(list!=NULL)
        {
            num++;
            list = list->next;
        }
        return num;
    }
};

你可能感兴趣的:(链表的转置问题)