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 0b88964

Browse files
feat: add golang solution for lcof problem 22-25
1 parent df2d452 commit 0b88964

File tree

6 files changed

+108
-0
lines changed

6 files changed

+108
-0
lines changed

‎lcof/面试题22. 链表中倒数第k个节点/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,29 @@ var getKthFromEnd = function(head, k) {
9696
};
9797
```
9898

99+
### Go
100+
101+
```go
102+
func getKthFromEnd(head *ListNode, k int) *ListNode {
103+
tmp := head
104+
for tmp != nil && k > 0{
105+
tmp = tmp.Next
106+
k--
107+
}
108+
slow := head
109+
fast := tmp
110+
for fast != nil {
111+
fast = fast.Next
112+
slow = slow.Next
113+
}
114+
return slow
115+
}
116+
```
117+
118+
119+
99120
### ...
121+
100122
```
101123
102124
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func getKthFromEnd(head *ListNode, k int) *ListNode {
2+
tmp := head
3+
for tmp != nil && k > 0{
4+
tmp = tmp.Next
5+
k--
6+
}
7+
slow := head
8+
fast := tmp
9+
for fast != nil {
10+
fast = fast.Next
11+
slow = slow.Next
12+
}
13+
return slow
14+
}

‎lcof/面试题24. 反转链表/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,29 @@ var reverseList = function(head) {
9090
};
9191
```
9292

93+
### Go
94+
95+
```Go
96+
func reverseList(head *ListNode) *ListNode {
97+
if head == nil ||head.Next == nil {
98+
return head
99+
}
100+
dummyHead := &ListNode{}
101+
cur := head
102+
for cur != nil {
103+
tmp := cur.Next
104+
cur.Next = dummyHead.Next
105+
dummyHead.Next = cur
106+
cur = tmp
107+
}
108+
return dummyHead.Next
109+
}
110+
```
111+
112+
113+
93114
### ...
115+
94116
```
95117
96118
```
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+
}

‎lcof/面试题25. 合并两个排序的链表/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,29 @@ var mergeTwoLists = function(l1, l2) {
136136
};
137137
```
138138

139+
### Go
140+
141+
```go
142+
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
143+
if l1 == nil {
144+
return l2
145+
}
146+
if l2 == nil {
147+
return l1
148+
}
149+
if l1.Val <= l2.Val {
150+
l1.Next = mergeTwoLists(l1.Next,l2)
151+
return l1
152+
}
153+
l2.Next = mergeTwoLists(l1, l2.Next)
154+
return l2
155+
}
156+
```
157+
158+
159+
139160
### ...
161+
140162
```
141163
142164
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
2+
if l1 == nil {
3+
return l2
4+
}
5+
if l2 == nil {
6+
return l1
7+
}
8+
if l1.Val <= l2.Val {
9+
l1.Next = mergeTwoLists(l1.Next,l2)
10+
return l1
11+
}
12+
l2.Next = mergeTwoLists(l1, l2.Next)
13+
return l2
14+
}

0 commit comments

Comments
(0)

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