forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path023 Swap Nodes in Pairs.py
41 lines (33 loc) · 1.03 KB
/
023 Swap Nodes in Pairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
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.
"""
__author__ = 'Danyang'
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def swapPairs(self, head):
"""
Linked List
:param head: ListNode
:return: ListNode
"""
dummy = ListNode(0)
dummy.next = head
pre = dummy
while pre.next and pre.next.next:
node1 = pre.next
node2 = pre.next.next
# temp = node2.next
# pre.next = node2
# node2.next = node1
# node1.next = temp
pre.next, node1.next, node2.next = node2, node2.next, node1
pre = pre.next.next
return dummy.next