C++ 链表 摘录

1/ 两两交换链表中的节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head==NULL)
        { return head; } 
        ListNode *dump=new ListNode(0); 
        dump->next=head; 
        head=dump; 
        // head 指向需要交换的两个节点 前一个节点。那么需要判断,head的后两个节点是否为空。
        while(head->next!=NULL&&head->next->next!=NULL)
        {
            ListNode* n1=head->next; 
            ListNode* n2=head->next->next;
            //交换,插入到head之后
            head->next=n2;
            n1->next=n2->next;
            n2->next=n1;
            //head 指向两个交换节点的后一个
            head=n1;
        }
        return dump->next;
    }
};

2/  k个一组 翻转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        // 确定翻转前的位置,pre 和翻转后的位置next;
        if(!head || k==1 ) return head;
        ListNode* dummy=new ListNode(-1),*pre=dummy,*cur=head;
        dummy->next=head;
        //cur 表示当前节点,从head 开始
        for(int i=1;cur;i++)
        {
            if(i%k == 0)
            {
                pre=reverse(pre,cur->next);
                cur=pre->next;
            }
            else
                cur=cur->next;
        }
        return dummy->next;
    }
    
    ListNode* reverse(ListNode* pre,ListNode* next)
    {// pre 为待翻转链表的前一个节点。
        ListNode* first=pre->next,*cur=first->next;
        while(cur!=next) // 停止条件
        {
            // 将cur 插入到 pre 后面
            first->next=cur->next;
            cur->next=pre->next;
            pre->next=cur;
            cur=first->next;
        }
        return first;
    }
};

3/

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-list
 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(!head) return NULL;
        ListNode* cur=head;
        int n=1;
        while(cur->next)
        {
            cur=cur->next;
            n++;
        }
        cur->next=head;
        int m=n-k%n;
        for(int i=0;i<m;i++)
        {
            cur=cur->next;
        }
        ListNode* newhead=cur->next;
        cur->next=NULL;
        return newhead;
    }
};

4/ 删除链表中重复元素II

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii
 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* dump=new ListNode(0);
        dump->next=head;
        
        ListNode* pre=dump;
        
        while(pre->next)
        {
            ListNode* cur=pre->next;
            while(cur->next && cur->next->val == cur->val)
                cur=cur->next;
            if(cur!=pre->next)// 向后移动了一次,有相等的元素
            {//直接将不相等的节点赋值到pre下一个节点
                pre->next=cur->next;
            }
            else // 正常后移
                pre=pre->next;
        }
        
        return dump->next;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值