当前位置:   article > 正文

链表刷题(c语言)_c语言链表刷题

c语言链表刷题

力扣

  1. struct ListNode* removeElements(struct ListNode* head, int val){
  2. struct ListNode* cur = head;
  3. struct ListNode* tail = NULL;
  4. while (cur)
  5. {
  6. if (cur->val == val)
  7. {
  8. struct ListNode* next = cur->next;
  9. if (tail == NULL)
  10. {
  11. head = next;
  12. }
  13. else
  14. {
  15. tail->next = cur->next;
  16. }
  17. free(cur);
  18. cur = next;
  19. }
  20. else
  21. {
  22. tail = cur;
  23. cur = cur->next;
  24. }
  25. }
  26. return head;
  27. }

链表中倒数第k个结点_牛客题霸_牛客网

  1. struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
  2. struct ListNode* slow = pListHead;
  3. struct ListNode* fast = slow;
  4. while(k--)
  5. {
  6. if(fast)
  7. fast = fast->next;
  8. else
  9. return NULL;
  10. }
  11. while(fast)
  12. {
  13. slow = slow->next;
  14. fast = fast->next;
  15. }
  16. return slow;
  17. }

链表分割_牛客题霸_牛客网

  1. class Partition {
  2. public:
  3. ListNode* partition(ListNode* pHead, int x) {
  4. ListNode* headless = (ListNode*)malloc(sizeof(ListNode));
  5. ListNode* headgreater = (ListNode*)malloc(sizeof(ListNode));
  6. headless->next = headgreater->next = NULL;
  7. ListNode* curless = headless;
  8. ListNode* curgreater = headgreater;
  9. ListNode* cur = pHead;
  10. while(cur)
  11. {
  12. if(cur->val < x)
  13. {
  14. curless->next = cur;
  15. curless = curless->next;
  16. }
  17. else
  18. {
  19. curgreater->next = cur;
  20. curgreater = curgreater->next;
  21. }
  22. cur = cur->next;
  23. }
  24. curgreater->next = NULL;
  25. curless->next = headgreater->next;
  26. pHead = headless->next;
  27. free(headless);
  28. free(headgreater);
  29. return pHead;
  30. }
  31. };

链表的回文结构_牛客题霸_牛客网

  1. class PalindromeList {
  2. public:
  3. bool chkPalindrome(ListNode* A) {
  4. ListNode* slow = A;
  5. ListNode* fast = A;
  6. while(fast && fast->next)
  7. {
  8. slow = slow->next;
  9. fast = fast->next->next;
  10. }
  11. ListNode* prev = NULL;
  12. ListNode* cur = slow;
  13. while(cur)
  14. {
  15. ListNode* next = cur->next;
  16. cur->next = prev;
  17. prev = cur;
  18. cur = next;
  19. }
  20. while(prev)
  21. {
  22. if(A->val == prev->val)
  23. {
  24. A = A->next;
  25. prev = prev->next;
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31. }
  32. return true;
  33. }
  34. };

力扣

  1. struct ListNode* reverseList(struct ListNode* head){
  2. struct ListNode* prev = NULL;
  3. struct ListNode* cur = head;
  4. while(cur)
  5. {
  6. struct ListNode* next = cur->next;
  7. cur->next = prev;
  8. prev = cur;
  9. cur = next;
  10. }
  11. return prev;
  12. }

力扣

  1. struct ListNode* middleNode(struct ListNode* head){
  2. struct ListNode* prev = head;
  3. struct ListNode* cur = head;
  4. while(cur && cur->next)
  5. {
  6. prev = prev->next;
  7. cur = cur->next->next;
  8. }
  9. return prev;
  10. }

力扣

  1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
  2. struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
  3. head->next = NULL;
  4. struct ListNode* tail = head;
  5. struct ListNode* curA = list1;
  6. struct ListNode* curB = list2;
  7. while(curA && curB)
  8. {
  9. if(curA->val < curB->val)
  10. {
  11. tail->next = curA;
  12. curA = curA->next;
  13. }
  14. else
  15. {
  16. tail->next = curB;
  17. curB = curB->next;
  18. }
  19. tail = tail->next;
  20. }
  21. if(curA == NULL)
  22. {
  23. tail->next = curB;
  24. }
  25. else
  26. {
  27. tail->next = curA;
  28. }
  29. struct ListNode* Head = head->next;
  30. free(head);
  31. return Head;
  32. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/618136
推荐阅读
相关标签
  

闽ICP备14008679号