c语言刷 链表题记录

61. 旋转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* rotateRight(struct ListNode* head, int k)
{
    if (head == NULL || head->next == NULL || k == 0) {
        return head;
    }
    int len = 0;
    struct ListNode* tail = head;
    while (tail && tail->next) {
        len++;
        tail = tail->next;
    }
    len++;

    if (k % len == 0) {
        return head;
    }
    
    k = len - (k % len);
    tail->next = head;

    while (k--) {
        tail = tail->next;
        head = head->next;
    }

    tail->next = NULL;
    return head;
}

148. 排序链表

暴力解法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

int Cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

struct ListNode* sortList(struct ListNode* head)
{
    int *arr = (int *)malloc(sizeof(int) * 50000);
    struct ListNode* tmp = head;
    int len = 0;
    while (tmp) {
        arr[len++] = tmp->val;
        tmp = tmp->next;
    }

    qsort(arr, len, sizeof(int), Cmp);
    
    tmp = head;
    for (int i = 0; i < len; i++) {
        tmp->val = arr[i];
        tmp = tmp->next;
    }

    return head;
}

剑指 Offer 24. 反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* stack[5001];
int len;

struct ListNode* reverseList(struct ListNode* head)
{
    if (head == NULL) {
        return NULL;
    }

    len = 0;
    struct ListNode* node = head;
    struct ListNode* res;
    // 入栈
    while (node) {
        stack[len++] = node;
        node = node->next;
    }

    // 弹栈
    len--;
    res = stack[len--];
    node = res;
    while (len >= 0) {
        node->next = stack[len--];
        node = node->next;
    }
    node->next = NULL;
    return res;
}

2. 两数相加

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    struct ListNode* head = NULL;
    struct ListNode* tail = NULL;
    int sum = 0;
    int carry = 0;

    while (l1 != NULL || l2 != NULL) {
        int l1Val = l1 ? l1->val : 0;
        int l2Val = l2 ? l2->val : 0;
        sum = l1Val + l2Val + carry;
        printf("sum(%d) = l1Val(%d) + l2Val(%d) + carry(%d)
", sum, l1Val, l2Val, carry);
        
        if (head == NULL) {
            head = malloc(sizeof(struct ListNode));
            tail = head;
            head->val = sum % 10;
            head->next = NULL;
        } else {
            tail->next = malloc(sizeof(struct ListNode));
            tail->next->val = sum % 10;
            tail = tail->next;
            tail->next = NULL;
        }

        carry = sum / 10;

        if (l1 != NULL) {
            l1 = l1->next;
        }
        if (l2 != NULL) {
            l2 = l2->next;
        }
    }

    if (carry > 0) {
        printf("%d
", carry);
        tail->next = malloc(sizeof(struct ListNode));
        tail->next->val = carry;
        tail = tail->next;
        tail->next = NULL;
    }

    return head;
}
原文地址:https://www.cnblogs.com/kongweisi/p/15382173.html