目录
3.给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
4.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
8.给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝
10..给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL
1.反转一个单链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
if(head==NULL||head->next==NULL)
{
return head;
}
struct ListNode* n1=NULL;
struct ListNode* n2=head;
struct ListNode* n3=head->next;
while(n2)
{
n2->next =n1;
n1=n2;
n2=n3;
if(n3)
n3=n3->next;
}
return n1;
}
2.删除链表中等于给定值 val 的所有节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
if(head==NULL)
return head;
struct ListNode*newhead=NULL,*ptail=NULL;
struct ListNode*ptail1=head,*ptail2=head->next;
while(ptail1)
{
if(ptail1->val!=val)
{
if(newhead==NULL)
{
newhead=ptail=ptail1;
}
else
{
ptail->next=ptail1;
ptail=ptail->next;
}
}
ptail1=ptail2;
if(ptail2)
ptail2=ptail2->next;
}
if(ptail)
ptail->next=NULL;
return newhead;
}
3.给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head) {
struct ListNode* n1=head;
struct ListNode* n2=head->next;
while(n2)
{
n1=n1->next;
n2=n2->next;
if(n2)
n2=n2->next;
}
return n1;
}
4.将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
if(list1==NULL&&list2==NULL)
return NULL;
struct ListNode*newhead=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode*ptail=newhead;
struct ListNode*ptail1=list1,*ptail2=list2;
while(ptail1&&ptail2)
{
if(ptail1->val<=ptail2->val)
{
ptail->next=ptail1;
ptail1=ptail1->next;
ptail=ptail->next;
}
else
{
ptail->next=ptail2;
ptail2=ptail2->next;
ptail=ptail->next;
}
}
if(ptail1)
ptail->next=ptail1;
if(ptail2)
ptail->next=ptail2;
return newhead->next;
}
5.输入一个链表,输出该链表中倒数第k个结点
面试题 02.02. 返回倒数第 k 个节点 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int kthToLast(struct ListNode* head, int k){
struct ListNode* ptail =head;
struct ListNode* prev =head;
while(k--)
{
ptail=ptail->next;
}
while(ptail)
{
prev=prev->next;
ptail=ptail->next;
}
return prev->val;
}
6.链表的回文结构
链表的回文结构_牛客题霸_牛客网 (nowcoder.com)
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
struct ListNode* middleNode(struct ListNode* head)
{
struct ListNode* n1 = head;
struct ListNode* n2 = head->next;
while (n2) {
n1 = n1->next;
n2 = n2->next;
if (n2)
n2 = n2->next;
}
return n1;
}
struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode* n1 = NULL;
struct ListNode* n2 = head;
struct ListNode* n3 = head->next;
while (n2) {
n2->next = n1;
n1 = n2;
n2 = n3;
if (n3)
n3 = n3->next;
}
return n1;
}
bool chkPalindrome(ListNode* A) {
struct ListNode* midnode = middleNode(A);
struct ListNode*newhead = reverseList(midnode);
struct ListNode* ptail1=A,*ptail2=newhead;
while(ptail2)
{
if(ptail1->val!=ptail2->val)
{
return false;
}
ptail1=ptail1->next;
ptail2=ptail2->next;
}
return true;
}
};
7.输入两个链表,找出它们的第一个公共结点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if(headA==NULL||headB==NULL)
{
return NULL;
}
struct ListNode *ptail1 =headA;
struct ListNode *ptail2 =headB;
int count1=0,count2=0;
while(ptail1)
{
count1++;
ptail1=ptail1->next;
}
while(ptail2)
{
count2++;
ptail2=ptail2->next;
}
ptail1=headA;
ptail2=headB;
int max=count1;
int min=count2;
int tmp=0;
int flag=1;
if(max<count2)
{
tmp=max;
max=min;
min=tmp;
flag=0;
}
int count3 =max-min;
//ptail1弄多的
if(flag==0)
{
struct ListNode * tmp =ptail1;
ptail1=ptail2;
ptail2=tmp;
}
while(count3--)
{
ptail1=ptail1->next;
}
while(ptail1)
{
if(ptail1==ptail2)
{
return ptail1;
}
ptail1=ptail1->next;
ptail2=ptail2->next;
}
return NULL;
}
8.给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝
/**
* Definition for a Node.
* struct Node {
* int val;
* struct Node *next;
* struct Node *random;
* };
*/
struct Node* copyRandomList(struct Node* head) {
if(head==NULL)
return NULL;
struct Node* cur =head;
struct Node* copy=NULL;
//拷贝每个节点并嵌入到两个原节点中间
while(cur)
{
copy =(struct Node*)malloc(sizeof(struct Node));
copy->val=cur->val;
copy->next=cur->next;
cur->next=copy;
cur=copy->next;
}
cur=head;
//处理random指针
while(cur)
{
copy=cur->next;
if(cur->random)
copy->random=cur->random->next;
else
copy->random=NULL;
cur=copy->next;
}
//穿起copy节点
cur=head->next;
struct Node* newhead=(struct Node*)malloc(sizeof(struct Node));
struct Node* ptail=newhead;
while(cur)
{
ptail->next=cur;
ptail=ptail->next;
if(cur->next)
cur=cur->next->next;
else
cur=NULL;
}
return newhead->next;
}
9.给定一个链表,判断链表中是否有环
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
if(head==NULL||head->next==NULL)
return false;
struct ListNode *ptail1=head;
struct ListNode *ptail2=head;
while(ptail2)
{
ptail1=ptail1->next;
if(ptail2)
ptail2=ptail2->next;
if(ptail2)
ptail2=ptail2->next;
if(ptail1==ptail2)
return true;
}
return false;
}
10..给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 NULL
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
if(head==NULL||head->next==NULL)
return NULL;
struct ListNode *ptail1=head;
struct ListNode *ptail2=head;
while(ptail2)
{
ptail1=ptail1->next;
if(ptail2)
ptail2=ptail2->next;
if(ptail2)
ptail2=ptail2->next;
if(ptail1==ptail2)
break;
}
if(ptail2==NULL)
return NULL;
ptail1=head;
while(1)
{
if(ptail1==ptail2)
{
return ptail1;
}
ptail1=ptail1->next;
ptail2=ptail2->next;
}
}