forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
24_Swap Nodes in Pairs.py
39 lines (34 loc) · 951 Bytes
/
24_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
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 15 16:49:03 2020
@author: leiya
"""
#date:0516
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
dummynode = ListNode(0)
dummynode.next = head
pre = dummynode
cur = head
while cur and cur.next:
fur = cur.next
pre.next = fur
pre = cur
cur.next = fur.next
cur = fur.next
fur.next = pre
return dummynode.next
#构建虚假表头+单指针
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
dummynode = ListNode(0)
pre = dummynode
pre.next = head
while pre.next is not None and pre.next.next is not None:
l1 = pre.next
l2 = pre.next.next
l1.next = l2.next
l2.next = l1
pre.next = l2
pre = l1
return dummynode.next