LeetCode● 24. 两两交换链表中的节点
为什么我这种方法超出时间限制了?啊!
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode *x1=head;
struct ListNode *x2=head->next;
struct ListNode *pre=head->next;
while(x1 && x2){
struct ListNode *temp=x2->next;
x2->next=x1;
x1->next=temp;
if(x1==head){
head=x2;
}
pre->next=x2;
//上述是交换,下述是移动
pre=x1;
x1=temp;
x2 = temp ? temp->next : NULL;
}
return head;
}
哈哈,检查出来了。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode *x1=head;
struct ListNode *x2=head->next;
struct ListNode *pre=head->next;
while(x1 && x2){
struct ListNode *temp=x2->next;
x1->next=temp;
x2->next=x1;//这部分有问题,头节点不用pre
if(x1!=head){
pre->next=x2;
}
if(x1==head){
head=x2;
}
//上述是交换,下述是移动
pre=x1;
x1=temp;
x2 = temp ? temp->next : NULL;
}
return head;
}
看了一下代码随想录的,它是弄了一个虚拟节点,然后不断重复,我的就直接暴力。两个的思考难度应该差不多,代码随想录的应该还是比我的难想一点,判断少一点
LeetCode ● 19.删除链表的倒数第N个节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
if (head == NULL) {
return head;
}
struct ListNode *p=head;
struct ListNode *q=head;
for(int i=1;i<n;i++){//没说要检查n有没有效,那就不检查了因为不知道返回什么
q=q->next;
}
if(q->next==NULL){
//n为链表大小
head=head->next;//这里需要加个return head;因为下面的q->next->next报错
//本来想着是到链表末尾就不会进去,就不需要了,跟我研究生复试要求不同
return head;
}
while(q->next->next){
q=q->next;
p=p->next;
}//之前搞了个虚拟头节点,但好像这个的没有设置malloc有函数,但不应该呀,但没办法我就只能去就只找到前一个节点了,下面进行删除
p->next=p->next->next;
return head;
}
LeetCode● 面试题 02.07. 链表相交
一开始没想到对齐,想不出来,就直接暴力解法:先两个都到达最后的节点并计数各自的链表长度,然后判断最后是否有所交点,最后为了循环找的时间复杂度少一点就找到小的直接一个一个的查找判断。
看了思路之后发现就是最后的一步进行优化,先对齐然后遍历判断。
/**
* 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;
}
int count1=1;
int count2=1;
struct ListNode *A=headA;
struct ListNode *B=headB;
while(A->next){
count1++;
A=A->next;
}
while(B->next){
count2++;
B=B->next;
}
if(A!=B){
return NULL;
}
/*优化:// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
swap (lenA, lenB);
swap (curA, curB);
}
不行,这是c++可以直接用的swap函数,c需要自己编写函数也挺简单的
*/
if(count1>count2){
int dif=count1-count2;
A=headA;
for(int i=0;i<dif;i++){
A=A->next;
}
B=headB;
while(A!=B){
A=A->next;
B=B->next;
}
}else{
int dif=count2-count1;
B=headB;
/*少些一点,但没必要 while (gap--) {
curA = curA->next;
}
*/
for(int i=0;i<dif;i++){
B=B->next;
}
A=headA;
while(A!=B){
A=A->next;
B=B->next;
}
}
return A;
}
LeetCode ● 142.环形链表II
幸好这平台自动保存了。这一题不会,完全没有做出来,只跟着文章解释通过。这就是我现在认清自己的原因,就是想不出来。小时候还认为自己很聪明呢(哈哈,自嘲)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode *fast = head, *slow = head;
while (fast && fast->next) {
// 这里判断两个指针是否相等,所以移位操作放在前面
slow = slow->next;
fast = fast->next->next;
if (slow == fast) { // 相交,开始找环形入口:分别从头部和从交点出发,找到相遇的点就是环形入口
struct ListNode *f = fast, *h = head;
while (f != h) f = f->next, h = h->next;
return h;
}
}
return NULL;
}