LC206. 翻转链表& LC24. 两两交换链表中的节点

206. 反转链表【简单】

图1

图1. 错误方法(添加头节点)

如图1所示,本题不需要创建虚拟头节点:若创建虚拟头节点dummy,最后翻转得到结果,节点1的下一个节点为dummy。但是按照题意,节点1下一个节点应该为null
在这里插入图片描述
图2. 正确方法示例

如上图2所示,我们首先令pre=null,保存一下cur.next节点(即temp节点),因为当cur.next指pre时,我们便无法通过cur.next获取到temp节点(因此需提前保存)。

        while(cur!=null){
            temp = cur.next;
            cur.next = pre;
            pre = cur;//1
            cur = next;//2  
            //代码片段1和2不可切换位置  
        }

完整代码如下:

 */ 递归法
class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null, head);
    }
    public ListNode reverse(ListNode pre, ListNode cur){
        if(cur == null)  return pre;
        ListNode next = cur.next;
        cur.next = pre;
        
        return reverse(cur, next);
    }
    
}   
    
    /*  迭代法
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode next = null;
        if (cur == null) return null;
        while(cur!=null){
            next = cur.next;
            cur.next = pre;
            pre = cur;//1
            cur = next;//2  
            //代码片段1和2不可切换位置  
        }
        return pre;
    }
    */

24. 两两交换链表中的节点【中等】

在这里插入图片描述
完整代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    //1、若链表长度为奇数2n+1,只需对前2n个节点交换,最后1个节点不做处理。
    public ListNode swapPairs(ListNode head) {
    //2、本题中设置虚拟头节点,头结点与索引为index=2的节点区别:无前前面的节点指向头结点,但对于index=2节点,会存在index=1节点指向。
        ListNode dummy = new ListNode(0);
        dummy.next = head;//不可少。
        ListNode prev = dummy;
        while(prev.next!=null && prev.next.next!=null){
            ListNode temp = head.next.next;
            prev.next = head.next;
            head.next.next = head;
            head.next = temp;
            prev = head; //而不是prev=temp
            head = head.next; 

        }
        return dummy.next;


    }
}

参考文献:
[1] 代码随想录 - 两两交换链表中的节点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是交换链表节点的C语言代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 定义交换链表节点的函数 void swapNodes(Node** head, int x, int y) { // 如果x和y相同,直接返回 if (x == y) { return; } // 定义需要交换节点和它们的前驱节点 Node *prevX = NULL, *currX = *head; Node *prevY = NULL, *currY = *head; // 寻找需要交换节点和它们的前驱节点 while (currX != NULL && currX->data != x) { prevX = currX; currX = currX->next; } while (currY != NULL && currY->data != y) { prevY = currY; currY = currY->next; } // 如果x或y不存在于链表,直接返回 if (currX == NULL || currY == NULL) { return; } // 如果x不是头节点,更新前驱节点的next指针 if (prevX != NULL) { prevX->next = currY; } else { // 如果x是头节点,更新头指针 *head = currY; } // 如果y不是头节点,更新前驱节点的next指针 if (prevY != NULL) { prevY->next = currX; } else { // 如果y是头节点,更新头指针 *head = currX; } // 交换节点的next指针 Node* temp = currY->next; currY->next = currX->next; currX->next = temp; } // 定义输出链表的函数 void printList(Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { // 创建一个测试用例链表 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = (Node*)malloc(sizeof(Node)); head->next->data = 2; head->next->next = (Node*)malloc(sizeof(Node)); head->next->next->data = 3; head->next->next->next = (Node*)malloc(sizeof(Node)); head->next->next->next->data = 4; head->next->next->next->next = NULL; // 输出原始链表 printf("原始链表: "); printList(head); printf("\n"); // 交换节点 swapNodes(&head, 2, 4); // 输出交换后的链表 printf("交换后的链表: "); printList(head); printf("\n"); return 0; } ``` 以上代码将创建一个测试用例链表,输出原始链表交换节点,输出交换后的链表。可以根据自己的需要修改测试用例链表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值