forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_simulation_n.py
More file actions
31 lines (27 loc) · 818 Bytes
/
Copy pathAC_simulation_n.py
File metadata and controls
31 lines (27 loc) · 818 Bytes
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: illuz <iilluzen[at]gmail.com>
# File: AC_simulation_n.py
# Create Date: 2015-03-05 14:37:17
# Usage: AC_simulation_n.py
# Descripton:
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# @param a ListNode
# @return a ListNode
def swapPairs(self, head):
dummy = ListNode(0)
dummy.next = head
cur = dummy
try:
while True:
pre, cur, nxt = cur, cur.next, cur.next.next
# change the position of cur and nxt
pre.next, cur.next, nxt.next = nxt, nxt.next, cur
# now cur is in the third place
except:
return dummy.next