您现在的位置是:首页 >学无止境 >力扣24题——两两交换链表中节点网站首页学无止境

力扣24题——两两交换链表中节点

の梦 2026-04-04 12:01:05
简介力扣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 {
    public ListNode swapPairs(ListNode head) {
        ListNode xuni=new ListNode(0,head);
        ListNode cur=xuni;
        while(cur.next!=null && cur.next.next!=null){
            ListNode node1 =cur.next;//第一个节点
            ListNode node2 =cur.next.next;//第二个节点
            cur.next=node2;
            node1.next=node2.next;
            node2.next=node1;
            cur=cur.next.next;
        }
        return xuni.next;
    }
}

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。