-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add golang solution for lcof problem 22-25
- Loading branch information
1 parent
df2d452
commit 0b88964
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
func getKthFromEnd(head *ListNode, k int) *ListNode { | ||
tmp := head | ||
for tmp != nil && k > 0{ | ||
tmp = tmp.Next | ||
k-- | ||
} | ||
slow := head | ||
fast := tmp | ||
for fast != nil { | ||
fast = fast.Next | ||
slow = slow.Next | ||
} | ||
return slow | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
func reverseList(head *ListNode) *ListNode { | ||
if head == nil ||head.Next == nil { | ||
return head | ||
} | ||
dummyHead := &ListNode{} | ||
cur := head | ||
for cur != nil { | ||
tmp := cur.Next | ||
cur.Next = dummyHead.Next | ||
dummyHead.Next = cur | ||
cur = tmp | ||
} | ||
return dummyHead.Next | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { | ||
if l1 == nil { | ||
return l2 | ||
} | ||
if l2 == nil { | ||
return l1 | ||
} | ||
if l1.Val <= l2.Val { | ||
l1.Next = mergeTwoLists(l1.Next,l2) | ||
return l1 | ||
} | ||
l2.Next = mergeTwoLists(l1, l2.Next) | ||
return l2 | ||
} |