Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode s

这篇博客讨论了LeetCode中的24题,即如何交换链表中的相邻节点并返回新的链表头。作者分享了自己在解决这个问题时遇到的困难,特别是关于如何在不修改链表值的情况下,仅通过改变节点本身来保持链表的连续性。通过使用额外的指针记录交换后的最后一个节点,作者最终解决了问题,并认识到将判断条件放入while循环中的巧妙之处。
订阅专栏 解锁全文
184

被折叠的 条评论
为什么被折叠?



