1. 程式人生 > >leetCode 24. Swap Nodes in Pairs (雙數交換節點) 解題思路和方法

leetCode 24. Swap Nodes in Pairs (雙數交換節點) 解題思路和方法

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, 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 swapPairs(ListNode head) {
	        
	        ListNode firstHead = new ListNode(0);
	        firstHead.next = head;
	        ListNode pre = firstHead;//定義一個頭結點,這樣所有的操作都相同
	        ListNode next = null;
	        while(head != null && head.next != null){
	        	//A-B-C-D交換BC,pre=A;B=head;C=next
	            next = head.next;//儲存交換的變數C
	            
	            head.next = next.next;//將B指向B的指標指向D
	            pre.next = next;//將A指向B的指標指向C
	            next.next = head;//將C指向D的指標指向B,完成交換,順序變為A-C-B-D
	            
	            //為下一迴圈準備變數
	            pre = head;//將pre變為B
	            head = head.next;//將head指向D
	            
	        }
	        return firstHead.next;
	    }
}


相關推薦

leetCode 24. Swap Nodes in Pairs (雙數交換節點) 解題思路方法

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

[leetcode]24. Swap Nodes in Pairs交換鏈表的節點

運行 鏈表 log tle first body blog 交換 ext 感覺這個題後臺的運行程序有問題,一開始自己想的是反轉鏈表那道題的方法,只是隔一個節點執行一次,但是沒有通過,TLE了,但是很奇怪,並沒有死循環,就是最後返回的時候超時。 最後的思路就是很簡單的進行交換

(Java) LeetCode 24. Swap Nodes in Pairs —— 兩兩交換鏈表中的節點

only title The reverse elf link 反轉鏈表 ould not Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1-&

LeetCode 24Swap Nodes in Pairs(兩兩交換連結串列中的節點)

Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the l

[LeetCode]24. Swap Nodes in Pairs兩兩交換鏈表中的節點

strong code linked 我們 nodes ace use style value Given a linked list, swap every two adjacent nodes and return its head. Example: Given

[LeetCode]24. Swap Nodes in Pairs兩兩交換連結串列中的節點

Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as

leetcode--(24. Swap Nodes in Pairs)

app modify spa ext div not != -- algorithm 描述: Given a linked list, swap every two adjacent nodes and return its head. For example,Given

LeetCode 24 Swap Nodes in Pairs

next 頭結點 code pair turn || lis AS urn public class SwapNodesInPairs { /** * Definition for singly-linked list. * public cl

leetcode#24 Swap Nodes in Pairs

null cpp 循環 def 使用 class ret 內部 2個 給定一個鏈表,兩兩交換其中相鄰的節點,並返回交換後的鏈表。 示例: 給定 1->2->3->4, 你應該返回 2->1->4->3. 說明: 你的算法只能使用常數的額外

[LeetCode] 24. Swap Nodes in Pairs

題:https://leetcode.com/problems/swap-nodes-in-pairs/description/ 題目 Given a linked list, swap every two adjacent nodes and return its head.

leetcode-24. Swap Nodes in Pairs

題目型別:連結串列 題意: 給出一個連結串列,按對交換節點, - 常數空間 - 不能用改變節點的值的方法做,需要交換節點本身。 我的思路:迭代。兩兩交換,修改next等指標 效率:10% 對於當前節點first,和下一個節點second - 記錄上一次

LeetCode--24. Swap Nodes in Pairs

題目連結:https://leetcode.com/problems/swap-nodes-in-pairs/ 思路類似於逆轉單鏈表(https://blog.csdn.net/To_be_to_thought/article/details/83930978)找出迴圈不變式 思路具體如圖:

[leetcode]24. Swap Nodes in Pairs

這題感覺沒什麼怎麼就medium了。 臨時加上一個空的頭結點,方便迴圈處理。 ListNode m=new ListNode(-1); m.next=head; head=m; m是要交換的兩個節點的前驅 p是要交換

python leetcode 24. Swap Nodes in Pairs

class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ ans=

leetcode-24-Swap Nodes in Pairs

The problem I did is very simple and clearly, which is seems stupid. I just point out its next point, next’s next point, and next’s next’s next

LeetCode 24 Swap Nodes in Pairs (C,C++,Java,Python)

Problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Giv

【python3】leetcode 24. Swap Nodes in Pairs (Medium)

24. Swap Nodes in Pairs (Medium) Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->

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

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

24.成對的交換節點24.Swap Nodes in Pairs

solution == cnblogs || des nbsp 位置 class 空間 題目: 給定一個鏈表,交換每兩個相鄰的節點並返回其頭。 例如,給定1->2->3->4,您應該返回列表2->1->4->3。 您的算法應該僅使用恒定空