63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

本文介绍了链表中三个核心操作:交换相邻节点、旋转链表及删除末尾指定节点的实现方法,提供了简洁高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

注意: 前两个互换的时候,head 要改变位置。还要有一个 pre 指针。

/**
 * 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) {
        ListNode *pre = NULL, *p = head;
        while(p && p->next) {
            ListNode *q = p->next;
            p->next = q->next;
            q->next = p;
            if(pre == NULL) head = q;
            else pre->next = q;
            pre = p;
            p = p->next;
        }
        return head;
    }
};

 

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.

注意: 前两个互换的时候,head 要改变位置。还要有一个 pre 指针。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
int getLength(ListNode *head) {
    int len = 0;
    while(head) {
        ++len;
        head = head->next;
    }
    return len;
}
class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k) {
        if(head == NULL) return NULL;
        k = k % getLength(head);
        if(k == 0) return head;
        ListNode *first, *second, *preFirst;
        first = second = head;
        for(int i = 1; i < k && second->next; ++i) // k-1 step
            second = second->next;
        //if(second->next == NULL) return head;
        while(second->next) {
            preFirst = first;
            first = first->next;
            second = second->next;
        }
        second->next = head;
        preFirst->next = NULL;
        return first;
    }
};

 

Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note: Given n will always be valid. Try to do this in one pass.

思路: 双指针。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        ListNode *pre = NULL, *p1, *p2;
        p1 = p2 = head;
        for(int i = 1; i < n; ++i) p2 = p2->next;
        while(p2->next) {
            pre = p1;
            p1 = p1->next;
            p2 = p2->next;
        }
        if(pre) pre->next = pre->next->next;
        return pre ? head : head->next;
        
    }
};

 

转载于:https://www.cnblogs.com/liyangguang1988/p/3961223.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值