LeetCode(24) - Swap Nodes in Pairs

  题目要求很简单,就是把list里面的node两两互换。

  当做比较复杂的LinkedList的题目的时候,最好是在草稿纸上画一画,走一遍流程,凭空想很容易会出错。这一题时LeetCode 25的k=2特殊例子,所以要简单很多。用两个node,一前一后,然后两两交换就好,细节上注意的是交换后两个node前面和后面是否各自连接上。最好自己在纸上走一遍,防止出错。

  代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode swapPairs(ListNode head) {
11         if (head == null) return head;
12         //一般需要处理head的时候会用到dumpnode,省下不少麻烦。
13         ListNode dumpNode = new ListNode(0);
14         dumpNode.next = head;
15         //需要交换的是prev.next和latt.next
16         ListNode prev = dumpNode;
17         ListNode latt = head;
18         while (latt != null && latt.next != null) {
19             //交换的方法很多,按照自己的思路用纸上走一遍,注意与前后的链接不要断掉。
20             prev.next = latt.next;
21             prev = prev.next;
22             latt.next = prev.next;
23             prev.next = latt;
24             prev = prev.next;
25             latt = latt.next;
26         }
27         return dumpNode.next;
28     }
29 }
时间: 2024-10-20 19:44:04

LeetCode(24) - Swap Nodes in Pairs的相关文章

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the

leetcode Linked List Swap Nodes in Pairs python 实现

题目: Swap Nodes in Pairs 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

LeetCode(24) Swap Nodes in Pairs

题目 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,

LeetCode解题报告--Swap Nodes in Pairs

题目: 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

Leetcode题目:Swap Nodes in Pairs

题目: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,

【LeetCode OJ】Swap Nodes in Pairs

题目: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,

leetcode 题解 || Swap Nodes in Pairs 问题

problem: 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

Leetcode-24 Swap Nodes in Pairs

#24. Swap Nodes in Pairs 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 t

leetCode 24. Swap Nodes in Pairs 链表

24. Swap Nodes in Pairs 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