From e152fb837ab4a369a0be791948522eeb172405bc Mon Sep 17 00:00:00 2001 From: tomato <18185657+taoyq1988@users.noreply.github.com> Date: Mon, 26 May 2025 21:01:27 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0147 No.0147.Insertion Sort List --- .../0147.Insertion Sort List/README.md | 36 +++++++++++++++++++ .../0147.Insertion Sort List/README_EN.md | 36 +++++++++++++++++++ .../0147.Insertion Sort List/Solution.go | 31 ++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 solution/0100-0199/0147.Insertion Sort List/Solution.go diff --git a/solution/0100-0199/0147.Insertion Sort List/README.md b/solution/0100-0199/0147.Insertion Sort List/README.md index 5f9c2b84a6d09..2ffa1f3e1ff77 100644 --- a/solution/0100-0199/0147.Insertion Sort List/README.md +++ b/solution/0100-0199/0147.Insertion Sort List/README.md @@ -181,6 +181,42 @@ var insertionSortList = function (head) { }; ``` +#### Go + +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func insertionSortList(head *ListNode) *ListNode { + if head == nil || head.Next == nil { + return head + } + dummy := &ListNode{head.Val, head} + pre, cur := dummy, head + for cur != nil { + if pre.Val <= cur.Val { + pre = cur + cur = cur.Next + continue + } + p := dummy + for p.Next.Val <= cur.Val { + p = p.Next + } + t := cur.Next + cur.Next = p.Next + p.Next = cur + pre.Next = t + cur = t + } + return dummy.Next +} +``` + diff --git a/solution/0100-0199/0147.Insertion Sort List/README_EN.md b/solution/0100-0199/0147.Insertion Sort List/README_EN.md index 7fc22c79e3e08..6501ceb997a91 100644 --- a/solution/0100-0199/0147.Insertion Sort List/README_EN.md +++ b/solution/0100-0199/0147.Insertion Sort List/README_EN.md @@ -171,6 +171,42 @@ var insertionSortList = function (head) { }; ``` +#### Go + +```go +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func insertionSortList(head *ListNode) *ListNode { + if head == nil || head.Next == nil { + return head + } + dummy := &ListNode{head.Val, head} + pre, cur := dummy, head + for cur != nil { + if pre.Val <= cur.Val { + pre = cur + cur = cur.Next + continue + } + p := dummy + for p.Next.Val <= cur.Val { + p = p.Next + } + t := cur.Next + cur.Next = p.Next + p.Next = cur + pre.Next = t + cur = t + } + return dummy.Next +} +``` + diff --git a/solution/0100-0199/0147.Insertion Sort List/Solution.go b/solution/0100-0199/0147.Insertion Sort List/Solution.go new file mode 100644 index 0000000000000..0bb4987697e5e --- /dev/null +++ b/solution/0100-0199/0147.Insertion Sort List/Solution.go @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func insertionSortList(head *ListNode) *ListNode { + if head == nil || head.Next == nil { + return head + } + dummy := &ListNode{head.Val, head} + pre, cur := dummy, head + for cur != nil { + if pre.Val <= cur.Val { + pre = cur + cur = cur.Next + continue + } + p := dummy + for p.Next.Val <= cur.Val { + p = p.Next + } + t := cur.Next + cur.Next = p.Next + p.Next = cur + pre.Next = t + cur = t + } + return dummy.Next +}