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 896885a

Browse files
feat: add solutions to lcof2 problems: No.024,025,026
1 parent 988142c commit 896885a

File tree

16 files changed

+755
-6
lines changed

16 files changed

+755
-6
lines changed

‎lcof2/剑指 Offer II 024. 反转链表/README.md‎

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,119 @@
5959

6060
### **Python3**
6161

62-
<!-- 这里可写当前语言的特殊实现逻辑 -->
63-
6462
```python
65-
63+
# Definition for singly-linked list.
64+
# class ListNode:
65+
# def __init__(self, x):
66+
# self.val = x
67+
# self.next = None
68+
69+
class Solution:
70+
def reverseList(self, head: ListNode) -> ListNode:
71+
pre, p = None, head
72+
while p:
73+
q = p.next
74+
p.next = pre
75+
pre = p
76+
p = q
77+
return pre
6678
```
6779

6880
### **Java**
6981

70-
<!-- 这里可写当前语言的特殊实现逻辑 -->
82+
迭代版本:
7183

7284
```java
85+
/**
86+
* Definition for singly-linked list.
87+
* public class ListNode {
88+
* int val;
89+
* ListNode next;
90+
* ListNode(int x) { val = x; }
91+
* }
92+
*/
93+
class Solution {
94+
public ListNode reverseList(ListNode head) {
95+
ListNode pre = null, p = head;
96+
while (p != null) {
97+
ListNode q = p.next;
98+
p.next = pre;
99+
pre = p;
100+
p = q;
101+
}
102+
return pre;
103+
}
104+
}
105+
```
106+
107+
递归版本:
108+
109+
```java
110+
/**
111+
* Definition for singly-linked list.
112+
* public class ListNode {
113+
* int val;
114+
* ListNode next;
115+
* ListNode(int x) { val = x; }
116+
* }
117+
*/
118+
class Solution {
119+
public ListNode reverseList(ListNode head) {
120+
if (head == null || head.next == null) {
121+
return head;
122+
}
123+
ListNode res = reverseList(head.next);
124+
head.next.next = head;
125+
head.next = null;
126+
return res;
127+
}
128+
}
129+
```
130+
131+
### **JavaScript**
132+
133+
```js
134+
/**
135+
* Definition for singly-linked list.
136+
* function ListNode(val) {
137+
* this.val = val;
138+
* this.next = null;
139+
* }
140+
*/
141+
/**
142+
* @param {ListNode} head
143+
* @return {ListNode}
144+
*/
145+
var reverseList = function (head) {
146+
let node = head;
147+
let pre = null;
148+
while (node) {
149+
let cur = node;
150+
node = cur.next;
151+
cur.next = pre;
152+
pre = cur;
153+
}
154+
return pre;
155+
};
156+
```
73157

158+
### **Go**
159+
160+
```go
161+
func reverseList(head *ListNode) *ListNode {
162+
if head == nil ||head.Next == nil {
163+
return head
164+
}
165+
dummyHead := &ListNode{}
166+
cur := head
167+
for cur != nil {
168+
tmp := cur.Next
169+
cur.Next = dummyHead.Next
170+
dummyHead.Next = cur
171+
cur = tmp
172+
}
173+
return dummyHead.Next
174+
}
74175
```
75176

76177
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public ListNode ReverseList(ListNode head) {
3+
ListNode newHead = null;
4+
while (head != null)
5+
{
6+
var next = head.next;
7+
head.next = newHead;
8+
newHead = head;
9+
head = next;
10+
}
11+
return newHead;
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func reverseList(head *ListNode) *ListNode {
2+
if head == nil ||head.Next == nil {
3+
return head
4+
}
5+
dummyHead := &ListNode{}
6+
cur := head
7+
for cur != nil {
8+
tmp := cur.Next
9+
cur.Next = dummyHead.Next
10+
dummyHead.Next = cur
11+
cur = tmp
12+
}
13+
return dummyHead.Next
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode reverseList(ListNode head) {
11+
ListNode pre = null, p = head;
12+
while (p != null) {
13+
ListNode q = p.next;
14+
p.next = pre;
15+
pre = p;
16+
p = q;
17+
}
18+
return pre;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function (head) {
13+
let node = head;
14+
let pre = null;
15+
while (node) {
16+
let cur = node;
17+
node = cur.next;
18+
cur.next = pre;
19+
pre = cur;
20+
}
21+
return pre;
22+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def reverseList(self, head: ListNode) -> ListNode:
9+
pre, p = None, head
10+
while p:
11+
q = p.next
12+
p.next = pre
13+
pre = p
14+
p = q
15+
return pre

‎lcof2/剑指 Offer II 025. 链表中的两数相加/README.md‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,115 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
利用栈将数字逆序。
60+
5961
<!-- tabs:start -->
6062

6163
### **Python3**
6264

6365
<!-- 这里可写当前语言的特殊实现逻辑 -->
6466

6567
```python
66-
68+
# Definition for singly-linked list.
69+
# class ListNode:
70+
# def __init__(self, x):
71+
# self.val = x
72+
# self.next = None
73+
74+
class Solution:
75+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
76+
s1, s2 = [], []
77+
while l1:
78+
s1.append(l1.val)
79+
l1 = l1.next
80+
while l2:
81+
s2.append(l2.val)
82+
l2 = l2.next
83+
carry, dummy = 0, ListNode(-1)
84+
while s1 or s2 or carry:
85+
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
86+
# 创建结点,利用头插法将结点插入链表
87+
node = ListNode(carry % 10)
88+
node.next = dummy.next
89+
dummy.next = node
90+
carry //= 10
91+
return dummy.next
6792
```
6893

6994
### **Java**
7095

7196
<!-- 这里可写当前语言的特殊实现逻辑 -->
7297

7398
```java
99+
/**
100+
* Definition for singly-linked list.
101+
* public class ListNode {
102+
* int val;
103+
* ListNode next;
104+
* ListNode(int x) { val = x; }
105+
* }
106+
*/
107+
class Solution {
108+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
109+
Deque<Integer> s1 = new ArrayDeque<>();
110+
Deque<Integer> s2 = new ArrayDeque<>();
111+
for (; l1 != null; l1 = l1.next) {
112+
s1.push(l1.val);
113+
}
114+
for (; l2 != null; l2 = l2.next) {
115+
s2.push(l2.val);
116+
}
117+
int carry = 0;
118+
ListNode dummy = new ListNode(-1);
119+
while (!s1.isEmpty() || !s2.isEmpty() || carry != 0) {
120+
carry += (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
121+
// 创建结点,利用头插法将结点插入链表
122+
ListNode node = new ListNode(carry % 10);
123+
node.next = dummy.next;
124+
dummy.next = node;
125+
carry /= 10;
126+
}
127+
return dummy.next;
128+
}
129+
}
130+
```
74131

132+
### **Go**
133+
134+
```go
135+
/**
136+
* Definition for singly-linked list.
137+
* type ListNode struct {
138+
* Val int
139+
* Next *ListNode
140+
* }
141+
*/
142+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
143+
s1, s2 := arraystack.New(), arraystack.New()
144+
for l1 != nil {
145+
s1.Push(l1.Val)
146+
l1 = l1.Next
147+
}
148+
for l2 != nil {
149+
s2.Push(l2.Val)
150+
l2 = l2.Next
151+
}
152+
carry, dummy := 0, new(ListNode)
153+
for !s1.Empty() || !s2.Empty() || carry > 0 {
154+
v, ok := s1.Pop()
155+
if ok {
156+
carry += v.(int)
157+
}
158+
v, ok = s2.Pop()
159+
if ok {
160+
carry += v.(int)
161+
}
162+
node := &ListNode{Val: carry % 10, Next: dummy.Next}
163+
dummy.Next = node
164+
carry /= 10
165+
}
166+
return dummy.Next
167+
}
75168
```
76169

77170
### **...**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
9+
s1, s2 := arraystack.New(), arraystack.New()
10+
for l1 != nil {
11+
s1.Push(l1.Val)
12+
l1 = l1.Next
13+
}
14+
for l2 != nil {
15+
s2.Push(l2.Val)
16+
l2 = l2.Next
17+
}
18+
carry, dummy := 0, new(ListNode)
19+
for !s1.Empty() || !s2.Empty() || carry > 0 {
20+
v, ok := s1.Pop()
21+
if ok {
22+
carry += v.(int)
23+
}
24+
v, ok = s2.Pop()
25+
if ok {
26+
carry += v.(int)
27+
}
28+
node := &ListNode{Val: carry % 10, Next: dummy.Next}
29+
dummy.Next = node
30+
carry /= 10
31+
}
32+
return dummy.Next
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11+
Deque<Integer> s1 = new ArrayDeque<>();
12+
Deque<Integer> s2 = new ArrayDeque<>();
13+
for (; l1 != null; l1 = l1.next) {
14+
s1.push(l1.val);
15+
}
16+
for (; l2 != null; l2 = l2.next) {
17+
s2.push(l2.val);
18+
}
19+
int carry = 0;
20+
ListNode dummy = new ListNode(-1);
21+
while (!s1.isEmpty() || !s2.isEmpty() || carry != 0) {
22+
carry += (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
23+
ListNode node = new ListNode(carry % 10);
24+
node.next = dummy.next;
25+
dummy.next = node;
26+
carry /= 10;
27+
}
28+
return dummy.next;
29+
}
30+
}

0 commit comments

Comments
(0)

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