[LeetCode] Swap Nodes in Pairs、Reverse Nodes in k-Group、Rotate List

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.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#define LN ListNode
class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        LN Guard(-1);
	    Guard.next=head;
	    LN* pre=&Guard;
	    LN* p1st;
	    LN* p2nd=&Guard;
	    do
	    {
		    p2nd=p2nd->next;
		    if( p2nd==NULL || p2nd->next==NULL ) //注意后面个条件
			    break;
		    p2nd=p2nd->next;
		    LN* tmp=p2nd->next;
		    p1st=pre->next;
		    pre->next=p2nd;
		    p2nd->next=p1st;
		    p1st->next=tmp;
		    pre=p2nd=p1st;
	    }while(1);
	    return Guard.next;
    }
};

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.

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

最近发现经常用do- while了,呵呵

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#define LN ListNode
class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        if (!head || k<=0)
		    return head;
	    LN* guard=new LN(-1);
	    guard->next=head;
	    LN* pre=guard;
	    LN* pKth=guard;
	    do
	    {
		    for(int i=0;i<k;i++)
		    {
			    if (!pKth)
				    break;
			    pKth=pKth->next;
		    }
		    if ( !pKth )
			    break;
		    _reverse(pre,pKth);
	    }while(1);
	    LN* ans=guard->next;
	    delete guard;
	    guard=0;
	    return ans;
    }
    
    void _reverse(LN*& pre,LN*& pKth)
    {
	    LN* pFirst=pre->next;
	    LN* pTail=pKth->next;
	    LN* pCur=pre->next;
	    pre->next=pTail;
	    pKth->next=NULL;
	    while(pCur)
	    {
		    LN* pTmp=pCur->next;
		    pCur->next=pre->next;
		    pre->next=pCur;
		    pCur=pTmp;
	    }
	    pre=pKth=pFirst;
    }
};

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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function

        if ( head==NULL)
		    return NULL;
	    int len=getListLength(head);
	    k=k%len;
	    if (k==0)
		    return head;

	    ListNode guard(-1);
	    guard.next=head;
	    ListNode* pTail=head;
	    ListNode* pTailPre= &guard;
	    while(k&&pTail)
	    {
		    k--;
		    pTail=pTail->next;
			pTailPre=pTailPre->next;
	    }

	    ListNode* pKth=head;
	    ListNode* pKthPre=&guard;
	    while(pTail)
	    {
		    pTail=pTail->next;
		    pTailPre=pTailPre->next;
		    pKth=pKth->next;
		    pKthPre=pKthPre->next;
	    }

	    ListNode* tmp =guard.next;
	    guard.next=pKth;
	    pKthPre->next=NULL;
	    pTailPre->next=tmp;
	    return guard.next;
    }

    int getListLength(ListNode* head)
    {
	    int len=0;
	    while(head)
	    {
		    len++;
		    head=head->next;
	    }
	    return len;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值