LeetCode 24.两两交换链表中的节点
struct ListNode* swapPairs(struct ListNode* head){
typedef struct ListNode ListNode;
ListNode* dummyhead = (ListNode*)malloc(sizeof(ListNode));
dummyhead->next = head;
ListNode* cur = dummyhead;
while (cur->next != NULL && cur->next->next != NULL)
{
ListNode* tmp = cur->next;
ListNode* tmp1 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = tmp;
cur->next->next->next = tmp1;
cur = cur->next->next;
}
return dummyhead->next;
}
下次整理吧
LeetCode 19.删除链表中的倒数第n个节点
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
typedef struct ListNode ListNode;
ListNode* dummyhead = (ListNode*)malloc(sizeof(ListNode));
dummyhead->next = head;
ListNode* fast = dummyhead;
ListNode* slow = dummyhead;
n++;
while (n-- && fast != NULL)
{
fast = fast->next;
}
while (fast != NULL)
{
fast = fast->next;
slow = slow->next;
}
ListNode* tmp = slow->next;
slow->next = slow->next->next;
free(tmp);
return dummyhead->next;
}
下次补一下
LeetCode 142.环形链表II
没做出来