Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 28af686

Browse files
committed
update linked-list 024
1 parent 2047347 commit 28af686

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

‎LeetCodeSolutions/024.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#==================================================
2+
#==> Title: 24. 两两交换链表中的节点
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date: 2/8/2021
7+
#==================================================
8+
9+
"""
10+
https://leetcode-cn.com/problems/swap-nodes-in-pairs/
11+
"""
12+
13+
14+
# Definition for singly-linked list.
15+
# class ListNode:
16+
# def __init__(self, val=0, next=None):
17+
# self.val = val
18+
# self.next = next
19+
class Solution:
20+
def swapPairs(self, head: ListNode) -> ListNode:
21+
if not (head and head.next):
22+
return head
23+
res = ListNode()
24+
pre = res
25+
pre.next = head
26+
while pre.next and pre.next.next:
27+
former, latter = pre.next, pre.next.next
28+
pre.next, former.next = latter, latter.next
29+
latter.next = former
30+
pre = former
31+
return res.next

‎graphic/linked-list.dot‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
digraph foo {
2+
node [shape = plaintext, label = "NULL"];
3+
null0;
4+
rankdir=LR;
5+
node [shape=record];
6+
a [label="{ <data> pre | <ref> }", width=1.2]
7+
b [label="{ <data> pre.next | <ref> }"];
8+
c [label="{ <data> pre.next.next | <ref> }"];
9+
a:ref:c -> b:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false, arrowsize=1.2];
10+
b:ref:c -> c:data [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
11+
c:ref:c -> null0 [arrowhead=vee, arrowtail=dot, dir=both, tailclip=false];
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /