Nav: << previous: 23.合并 K 个升序链表 | next: 25.K 个一组翻转链表 >>


Description

tab: English
 
<p>Given a&nbsp;linked list, swap every two adjacent nodes and return its head. You must solve the problem without&nbsp;modifying the values in the list&#39;s nodes (i.e., only nodes themselves may be changed.)</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">head = [1,2,3,4]</span></p>
 
<p><strong>Output:</strong> <span class="example-io">[2,1,4,3]</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg" style="width: 422px; height: 222px;" /></p>
</div>
 
<p><strong class="example">Example 2:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">head = []</span></p>
 
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
 
<p><strong class="example">Example 3:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">head = [1]</span></p>
 
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
 
<p><strong class="example">Example 4:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">head = [1,2,3]</span></p>
 
<p><strong>Output:</strong> <span class="example-io">[2,1,3]</span></p>
</div>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li>The number of nodes in the&nbsp;list&nbsp;is in the range <code>[0, 100]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 100</code></li>
</ul>
 
 
 
---
 
[submissions](https://leetcode.com/problems/swap-nodes-in-pairs/submissions/) | [solutions](https://leetcode.com/problems/swap-nodes-in-pairs/solutions/)
 
 
tab: 中文
 
<p>给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>输入:</strong>head = [1,2,3,4]
<strong>输出:</strong>[2,1,4,3]
</pre>
 
<p><strong>示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>head = []
<strong>输出:</strong>[]
</pre>
 
<p><strong>示例 3:</strong></p>
 
<pre>
<strong>输入:</strong>head = [1]
<strong>输出:</strong>[1]
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li>链表中节点的数目在范围 <code>[0, 100]</code> 内</li>
	<li><code>0 &lt;= Node.val &lt;= 100</code></li>
</ul>
 
 
 
---
 
[提交记录](https://leetcode.cn/problems/swap-nodes-in-pairs/submissions/) | [题解](https://leetcode.cn/problems/swap-nodes-in-pairs/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122