1. 程式人生 > >演算法第五週Swap Nodes in Pairs[medium]

演算法第五週Swap Nodes in Pairs[medium]

Swap Nodes in Pairs[medium]

Description

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.

Analysis

題目是很容易理解的。就是將相鄰的結點交換,並且題目中明確要求不能直接交換結點的值,同時,從題目中我們能夠看出他是每兩個結點進行一次交換。我採用的方法是每三個結點交換後兩個,首先建立一個結點h,它的next指標指向list的head,如果h的下一個和下下個均不為空,我們即可利用h來交換後兩個結點,之後更新h的值,是他等於上一次處理的最後一個結點。

Solution

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next
; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* swapPairs(ListNode* head) { ListNode* h = new ListNode(1); h->next = head; ListNode* c = h; ListNode* p = NULL; ListNode* t = NULL; while (c->next != NULL
&&c->next->next != NULL) { p = c->next; t = c->next->next; p->next = t->next; c->next = t; t->next = p; c = p; } return h->next; } };

相關推薦

演算法Swap Nodes in Pairs[medium]

Swap Nodes in Pairs[medium] Description Given a linked list, swap every two adjacent nodes and return its head. For example,

[每日演算法] leetcode24題 Swap Nodes in Pairs 111題 Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree 原題目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along

24. Swap Nodes in Pairs - Medium

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 2->1-&g

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

四周演算法分析與設計:Swap Nodes in Pairs

演算法描述 Given a linked list, swap every two adjacent nodes and return its head. For example,

演算法38--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 2-&

leetcode鏈表--16、swap-nodes-in-pairs(成對交換鏈表結點)

ive push 返回 pre head 交換 while const int 題目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given

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

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

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

24. Swap Nodes in Pairs

ext this next air last lis list node 如果 24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. F

[Leetcode] Swap Nodes in Pairs

ace tps modify problem leetcode pan emp front 順序 Swap Nodes in Pairs 題解 原創文章,拒絕轉載 題目來源:https://leetcode.com/problems/swap-nodes-in-pairs/

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

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

【Leetcode】24. Swap Nodes in Pairs

ace 一個 改變 swa 不改變 鏈表 tip not script Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2

LC_24. Swap Nodes in Pairs

log des desc pop link ppa script rip lis https://leetcode.com/problems/swap-nodes-in-pairs/description/ Given a linked list, swap every t

LeetCode 24 Swap Nodes in Pairs

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

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

24.Swap Nodes in Pairs

info res public int ret lap ppa one 代碼 題目鏈接 題目大意:交換單鏈表中的相鄰結點。例子如下: 法一:交換兩個相鄰的值,不交換相鄰結點。代碼如下(耗時3ms): 1 public ListNode swapPairs(L

LeetCode算法題python解法:24. Swap Nodes in Pairs

etc while bin pytho append pen usr and 特殊情況 原題: Given a linked list, swap every two adjacent nodes and return its head. Example: Given

leetcode#24 Swap Nodes in Pairs

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

[Swift]LeetCode24. 兩兩交換連結串列中的節點 | 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 2->1-&g