LeetCode【链表】

public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummy = new ListNode(0,head);
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = dummy;
        while (cur != null){
            stack.push(cur);
            cur = cur.next;
        }

        for (int i = 0; i < n; i++){
            stack.pop();
        }
        ListNode prev = stack.peek();
        prev.next = prev.next.next;
        return dummy.next;
    }

 public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

     if (list1 == null){
            return list2;
        }
        if (list2 == null){
            return list1;
        }

        if (list1.val < list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else {
            list2.next = mergeTwoLists(list1,list2.next);
            return list2;
        }
    }

 public ListNode swapPairs(ListNode head) {
        
        if (head == null){
            return null;
        }
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode tmp = newHead;
        while (tmp.next != null && tmp.next.next != null){
            ListNode node1 = tmp.next;
            ListNode node2 = tmp.next.next;
            tmp.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            tmp = node1;
        }
        return newHead.next;
    }

  public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0){
            return head;
        }
        int count = 1;
        ListNode tmp = head;
        while (tmp.next != null){
            count++;
            tmp = tmp.next;
        }
        k %= count;
        if (k == 0){
            return head;
        }
        //首尾相连
        tmp.next = head;
        for (int i = 0;
### LeetCode 链表题目 ACM模式 解题方法 #### 了解ACM模式下的链表处理方式 在LeetCode平台,当采用ACM模式来解答链表相关的问题时,意味着提交的解决方案不仅限于函数定义部分,而是完整的程序文件。这通常包含了必要的头文件引入、`main()`函数以及输入输出逻辑的设计[^4]。 对于链表操作而言,在编写代码前理解其基本概念至关重要。例如,单向链表由节点组成,每个节点包含两部分内容:存储数据项的数据域和指向下一个节点地址指针域;双向链表则在此基础上增加了向前遍历的功能。为了高效地解决问题,熟悉如何创建新节点、连接或断开节点间的联系是非常重要的[^3]。 #### 完整的ACM模式链表问题求解框架 下面给出一个通用模板用于解决大多数基于C++语言编写的LeetCode链表类题目: ```cpp #include <iostream> using namespace std; // Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: // 主要算法实现放在这里 }; int main() { // 输入读取与初始化工作 string input; cin >> input; // 将输入转换成链表形式或其他所需格式 // 调用Solution类的方法并获取返回结果 // 输出最终答案到标准输出流cout中去 return 0; } ``` 此模板展示了从接收用户输入直到打印出计算结果的整体流程。其中具体业务逻辑需根据实际问题调整完善。 #### 实战案例分析——环形链表检测 作为实例之一,“环形链表”是一个经典面试题,它要求判断给定链表是否存在循环结构。一种有效策略是利用快慢指针法(Floyd判圈算法),即设置两个速度不同的指针同时沿着链表前进直至相遇,则表明存在闭环路径。 ```cpp bool hasCycle(ListNode *head) { if (!head || !head->next) return false; ListNode* slow = head; ListNode* fast = head->next; while (fast != nullptr && fast->next != nullptr){ if(slow == fast) return true; slow = slow->next; fast = fast->next->next; } return false; } ``` 上述代码片段实现了对链表是否有环这一特性的快速检验功能,并且能够很好地适应ACM竞赛环境的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-孤单又灿烂的神-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值