1.删除单链表的重复结点
力扣链接
struct ListNode* removeDuplicateNodes(struct ListNode* head)
{
struct ListNode *current = head;
while(current)
{
struct ListNode *p = current;
while(p->next)
{
if (p->next->val == current->val)
{
p->next = p->next->next;
}
else
{
p = p->next;
}
}
current = current->next;
}
return head;
}
{
int index[20001] = {0};
struct ListNode *pre = head;
struct ListNode *current = pre->next;
index[head->val] = 1;
while (current)
{
if (index[current->val] == 0)
{
index[current->val] = 1;
pre = current;
current = current->next;
}
else
{
pre->next = current->next;
current = pre->next;
}
}
return head;
}
2.找出链表的倒数第K个元素
力扣链接
struct ListNode* getKthFromEnd(struct ListNode* head, int k)
{
struct ListNode* fast = head;
struct ListNode* slow = head;
while(k--)
{
fast = fast->next;
}
while(fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
3.找出链表的中间节点
力扣链接
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* fast= head;
struct ListNode* slow= head;
while(fast != NULL && fast->next !=NULL)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* fast = head;
struct ListNode* latter = head;
while(fast->next != NULL && fast->next->next !=NULL)
{
fast = fast ->next->next;
slow = slow->next;
}
return slow;
}
4.反转链表
力扣链接
struct ListNode* reverseList(struct ListNode* head)
{
if(head == NULL)
{
return head;
}
struct ListNode* former = NULL;
struct ListNode* mid = head;
struct ListNode* latter = NULL;
while(mid != NULL)
{
former = mid->next;
mid->next = latter;
latter = mid;
mid = former;
}
return latter;
}
5.判断环形链表
力扣链接
bool hasCycle(struct ListNode *head)
{
if (NULL == head || NULL == head->next)
{
return false;
}
struct ListNode * slow=head;
struct ListNode * fast=head->next;
while (fast && fast->next)
{
if(slow == fast)
{
return true;
}
else
{
slow=slow->next;
fast=fast->next->next;
}
}
return false;
}
6.链表相交
力扣链接
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
if(headA == NULL || headB == NULL)
{
return NULL;
}
int length_a = 1;
int length_b = 1;
int length = 0;
struct ListNode *a = headA;
struct ListNode *b = headB;
while(a->next)
{
a = a->next;
length_a++;
}
while(b->next)
{
b = b->next;
length_b++;
}
if(&*a == &*b)
{
a = headA;
b = headB;
length = length_a - length_b;
if (length > 0)
{
while(length--)
{
a = a->next;
}
}
else
{
length = abs(length);
while(length--)
{
b = b->next;
}
}
while (&*a != &*b)
{
a = a->next;
b = b->next;
}
return a;
}
else
{
return NULL;
}
}
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{
struct ListNode *p=headA;
struct ListNode *q=headB;
while(p!=q)
{
if (p==NULL)
p=headB;
else
p=p->next;
if (q==NULL)
q=headA;
else
q=q->next;
}
return q;
}
7.判断回文链表
力扣链接
bool isPalindrome(struct ListNode* head)
{
struct ListNode *fast = head;
struct ListNode *slow = head;
while (fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
}
struct ListNode *left = NULL;
struct ListNode *current = slow;
struct ListNode *right = NULL;
while (current != NULL)
{
right = current->next;
current->next = left;
left = current;
current = right;
}
fast = head;
while (left)
{
if(fast->val != left->val)
{
return false;
}
left = left->next;
fast = fast->next;
}
return true;
}
8.合并两个有序链表
力扣链接
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode *a = l1;
struct ListNode *b = l2;
struct ListNode *head = malloc(sizeof(struct ListNode));
struct ListNode *current = head;
if (head == NULL)
{
return NULL;
}
while(a && b)
{
if(a->val <= b->val)
{
current->next = a;
a = a->next;
}
else
{
current->next = b;
b = b->next;
}
current = current->next;
}
current->next = (NULL == a ? b : a);
return head->next;
}