链表oj题(第一弹)
创始人
2024-04-09 13:14:53
0

通过前两篇博客我们了解了链表的实现,那么今天我们来看看链表的oj题是如何完成的。

1、移除链表元素

 题目要求我们删掉与val相同的节点。

方法一:我们可以写一个循环,首先创建两个节点,一个头节点,一个尾节点,开始时为空指针,将不为val的值尾插到新的尾后,当值与val相等,我们先创建next将cur释放掉后直接将next赋给cur即可。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{if(head == NULL){return NULL;}struct ListNode* newhead,*newtail;newhead = newtail = NULL;struct ListNode* cur = head;while(cur){if(cur->val != val){if(newtail == NULL){newhead = newtail = cur;}else{                newtail->next = cur;newtail = newtail->next;}cur = cur->next;}else{struct ListNode* next = cur->next;free(cur);cur = next;}}if(newtail){newtail->next =NULL;}return newhead;
}

方法二:我们可以遍历链表将val相等的位置直接释放即可。这种方法别的地方也叫做迭代法。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{while (NULL != head && head->val == val) {head = head->next;}if (NULL == head) {return head;}struct ListNode* cur = head;struct ListNode* prev = head;while(cur){if(cur->val == val){//找到前一个while(prev->next->val != val){prev = prev->next;}struct ListNode* next = cur->next;prev->next = next;free(cur);cur = next;}else{cur = cur->next;}        }return head;
}

2、反转链表

这道题我们先创建一个结构体指针指向链表的头指针然后头插到新的链表头,最后返回新的链表头。

struct ListNode* reverseList(struct ListNode* head)
{struct ListNode* newhead = NULL;struct ListNode* tail = head;while(tail){struct ListNode* next = tail->next;tail->next = newhead;newhead = tail;tail = next;}return newhead;
}

 3、返回链表中间节点

这道题我们可以使用快慢指针的方法,定义两个结构体指针。快指针走两步,慢指针走一步。

 

 对于条件的控制我们应该区分出链表节点个数的奇偶性,当链表节点个数为奇数时fast应不能为空指针,当个数为偶数时fast的next不能为空。

struct ListNode* middleNode(struct ListNode* head)
{struct ListNode* fast = head;struct ListNode* slow = head;while(head&&head->next){head = head->next->next;slow = slow->next;}return slow;
}

 4、返回链表倒数第k个节点

 这道题依旧是快慢指针,但与上一道题不同。我们先让fast指针走k步,然后两个指针同时走。直到fast为空,返回slow。

 

 
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) 
{// write code hereif(pListHead == NULL){return NULL;}struct ListNode* fast = pListHead;struct ListNode* slow = pListHead;while(k){if(fast ==NULL)return NULL;fast = fast->next;k--;}while(fast){fast = fast->next;slow = slow->next;}return slow;
}

5、链表分割

这道题我们可以利用带哨兵位的头节点来解,malloc两个头节点两个尾节点,一个用来存放大于等于x的节点,一个用来存放小于x的节点。最后将两个链表连起来,再释放掉哨兵位。返回头指针即可。

class Partition {
public:ListNode* partition(ListNode* pHead, int x) {// write code herestruct ListNode* bighead,*bigtail,*littlehead,*littletail;bighead = bigtail =(struct ListNode*) malloc(sizeof(struct ListNode));littlehead = littletail =(struct ListNode*) malloc(sizeof(struct ListNode));struct ListNode* cur = pHead;while(cur){if(cur->valnext = cur;littletail = cur;                }else{bigtail->next = cur;bigtail = cur;             }cur = cur->next;}littletail->next = bighead->next;bigtail->next = NULL;pHead = littlehead->next;free(littlehead);free(bighead);return pHead;}
};

 6、合并链表

 此题我们可以将l2中的节点放入l1中,只需要一个一个比较、插入即可。

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{struct ListNode* head1 = NULL;struct ListNode* tail1 = NULL;if(list1 == NULL){return list2;}if(list2 == NULL){return list1;}while(list1&&list2){if(list1->valval){if(tail1 == NULL){head1 = tail1 = list1;}else{tail1->next = list1;tail1 = tail1->next;}list1 = list1->next;}else{if(tail1 == NULL){head1 = tail1 = list2;}else{tail1->next = list2;tail1 = tail1->next;}list2 = list2->next;}}if(list1)tail1->next = list1;if(list2)tail1->next = list2;return head1;
}

相关内容

热门资讯

民政部:会同有关部门建立最低生... 据新华社,记者12月30日在全国民政工作会议上获悉,民政部将会同有关部门建立最低生活保障标准备案制度...
肯尼亚投资:税务及法律合规指引 一、肯尼亚的外国直接投资 肯尼亚无疑是非洲吸引外国直接投资(FDI)最多的国家之一。根据《2025年...
大同多部门联动打击生态环境违法... 本报讯(通讯员刘美 陈俊宏)近日,大同市中级人民法院联合大同市人民检察院、大同市公安局、大同市司法局...
南阳宛城检察:让道争执酿祸端 ... 大象新闻记者 张定有 通讯员 魏颖 张婷/文图 一桩因乡间小道通行引发的争执,险些酿成极端事件。南阳...
寻找靠谱征地律师,孙侠律师 在征地相关法律事务中,找到一位靠谱且成功率高的征地律师至关重要。随着城市化进程的加速,征地纠纷日益增...
民政部:会同有关部门建立最低生... 记者12月30日在全国民政工作会议上获悉,民政部将会同有关部门建立最低生活保障标准备案制度,从制度上...
秘鲁无刺蜂成为全球首个获法律权... 气候变化、杀虫剂及入侵物种正威胁授粉昆虫生存之际,亚马逊无刺蜂在秘鲁正式获得法律权利。 秘鲁亚马逊...
最高法发布关于部分民事案件管辖... 新华社北京12月30日电(记者冯家顺)最高人民法院12月30日公开发布《最高人民法院关于部分民事案件...
2026年河北省高职单招政策出... 高职单招成为职业院校招生主渠道,计划申报比例原则上不低于本校2025年度高职(专科)招生总计划的70...