Skip to content

Commit 5b69c5c

Browse files
committed
swap nodes update
1 parent ef2597d commit 5b69c5c

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
2121
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/#/description) | [Python](./linkedlist/addTwoNumbers.py) | _O(n)_| _O(n)_ | Medium |CC189| |
2222
|445| [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/#/description)| [Python](./linkedlist/addTwoNumbersTwo.py) | _O(n)_| _O(n)_ | Medium |CC189 |Stack|
2323

24-
## Daily Task
24+
## 4/14 Tasks (LinkedList Medium)
2525
| # | Title | Solution | Time | Space | Difficulty |Tag| Note|
2626
|-----|-------| -------- | ---- | ------|------------|---|-----|
2727
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/#/description)| [Python](./linkedlist/detectCycleii.py) | _O(n)_| _O(1)_ | Medium |CC189 |[Video](https://www.youtube.com/watch?v=iZVBVCpmugI&t=1s)|
2828
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/#/description)| [Python](./linkedlist/oddEvenList.py) | _O(n)_| _O(1)_ | Medium | ||
29-
|143|[Reorder List](https://leetcode.com/problems/reorder-list/#/description)| [Python](./linkedlist/reorderList.py) | _O(n)_| _O(1)_ | Medium | ||
29+
|143|[Reorder List](https://leetcode.com/problems/reorder-list/#/description)| [Python](./linkedlist/reorder.py) | _O(n)_| _O(1)_ | Medium | ||
30+
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/#/solutions)| [Python](./linkedlist/swapPairs.py) | _O(n)_| _O(1)_ | Medium | ||

linkedlist/swapPairs.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 24. Swap Nodes in Pairs
2+
# Author: Yu Zhou
3+
4+
# Given a linked list, swap every two adjacent nodes and return its head.
5+
#
6+
# For example,
7+
# Given 1->2->3->4, you should return the list as 2->1->4->3.
8+
#
9+
# 思路
10+
11+
# 这题的关键是处理每个Node的指针关系
12+
# 设置3个pointer, p0也就是prev,p1是head, p2是next
13+
# 然后把关系通过纸画下来,就能够理解个个pointer的关系了。
14+
# 之前我的想法特别奇葩,在下面的代码里,感觉也没写对。。
15+
16+
# ****************
17+
# Final Solution *
18+
# ****************
19+
class Solution(object):
20+
def swapPairs(self, head):
21+
if not head:
22+
return head
23+
24+
guard = p0 = ListNode(0)
25+
guard.next = head
26+
27+
while p0.next and p0.next.next:
28+
p1 = p0.next #第一个交换数
29+
p2 = p0.next.next #第2个交换数
30+
31+
p0.next = p2
32+
p1.next = p2.next
33+
p2.next = p1
34+
35+
p0 = p0.next.next
36+
37+
return guard.next
38+
39+
# ***************************************
40+
# The following code is an fail attempt *
41+
# ***************************************
42+
class Solution(object):
43+
def swapPairs(self, head):
44+
if not head:
45+
return head
46+
47+
dummy1 , dummy2 = ListNode(0), ListNode(0)
48+
49+
cur = head.next
50+
while cur:
51+
dummy1.next = cur
52+
if cur.next:
53+
cur = cur.next.next
54+
55+
cur = head
56+
while cur and cur.next:
57+
dumm2.next = cur
58+
cur = cur.next.next
59+
60+
#merge
61+
dummy = ListNode(0)
62+
dummy.next = head
63+
while dummy1 and dummy2:
64+
dummy.next = dummy1
65+
dummy = dummy.next
66+
dummy1 = dummy1.next
67+
68+
dummy.next = dummy2
69+
dummy = dummy.next
70+
dummy2 = dummy2.next
71+
72+
if dummy1:
73+
dummy.next = dummy1
74+
if dummy2:
75+
dummy.next = dummy2
76+
77+
return dummy.next

0 commit comments

Comments
 (0)