LeetCode_Swap Nodes in Pairs

Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.
(交换链表中相邻的两个值)

Note:

Your algorithm should use only constant extra space.
You may not modify the values in the list’s nodes, only nodes itself may be changed.

Example:



链表指针

这个题是一个很直观的指针问题,在设计算法的过程中将指针的变换在纸上设计出来即可得出算法,具体实现过程如下:
(引入头指针是一个链表中常用的方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
newNode = ListNode(0)
newNode.next = head

p = newNode
while p.next and p.next.next:
q = p.next
p.next = q.next
q.next = p.next.next
p.next.next = q
p = q

return newNode.next