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 values in the list, only nodes itself can be changed.

代码:oj测试164ms通过

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution:
 8     # @param a ListNode
 9     # @return a ListNode
10     def swapPairs(self, head):
11
12         if head is None or head.next is None:
13             return head
14
15         dummyhead = ListNode(0)
16         dummyhead.next = head
17         p = dummyhead
18
19         while p.next is not None and p.next.next is not None:
20             tmp = p.next.next.next
21             p.next.next.next = p.next
22             p.next = p.next.next
23             p.next.next.next = tmp
24             p = p.next.next
25
26
27         return dummyhead.next

思路

1. 建立一个虚表头hummyhead 这样方便操作一些

2. p.next始终指向要交换的下一个元素的位置

3. 先保存p.next.next.next,再移动p,p.next,p.next.next,p.next.next.next:先动p.next.next.next再动其他的。

小白我一开始先动的是p,p.next结果后面的p.next.next就丢了,其他小白别陷入这个误区,高手请略过。

Tips: 动了哪个指针,就把哪个指针上面打个×;添加了哪个指针,就在两个点之间加一根线;画画图就出来了,别光看着不动笔。

时间: 2024-08-08 01:26:22

leetcode Linked List Swap Nodes in Pairs python 实现的相关文章

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解题报告--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】24. Swap Nodes in Pairs

题目描述: Given a linked list, swap every two adjacent nodes and return its head. 解题分析: 解题思路很简单,就是先两两扫描,然后调换顺序即可.这道题的难点在于每个节点的next域指向问题,所以解这样的题最好可以在纸上写一下交换的步骤就不难实现 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val

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,

[Linked List]Swap Nodes in Pairs

Total Accepted: 73777 Total Submissions: 219963 Difficulty: Medium 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

【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 Swap Nodes in Pairs python

# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if hea

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 Li