mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-02 05:51:34 +00:00
添加Go语言题解
This commit is contained in:
@@ -173,6 +173,30 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func isPalindrome(head *ListNode) bool {
|
||||
// 将节点中的值按顺序放在arr中。
|
||||
arr := []int{}
|
||||
node := head
|
||||
for node != nil {
|
||||
arr = append(arr, node.Val)
|
||||
node = node.Next
|
||||
}
|
||||
// 双指针判断是否为回文
|
||||
l, r := 0, len(arr) - 1
|
||||
for l < r {
|
||||
if arr[l] != arr[r] {
|
||||
return false
|
||||
}
|
||||
l++
|
||||
r--
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
这个方法可以直接通过,但是这个方法需要辅助数组,那我们还有其他更好的方法吗?
|
||||
|
||||
**双指针翻转链表法**
|
||||
@@ -201,7 +225,7 @@ class Solution {
|
||||
ListNode backhalf = reverse(midnode.next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
ListNode p1 = head;
|
||||
ListNode p2 = backhalf;
|
||||
ListNode p2 = backhalf;
|
||||
while (p2 != null) {
|
||||
if (p1.val != p2.val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
@@ -210,11 +234,11 @@ class Solution {
|
||||
}
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode.next = reverse(backhalf);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
//找到中点
|
||||
public ListNode searchmidnode (ListNode head) {
|
||||
@@ -223,7 +247,7 @@ class Solution {
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
//翻转链表
|
||||
@@ -258,7 +282,7 @@ public:
|
||||
ListNode * backhalf = reverse(midnode->next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
ListNode * p1 = head;
|
||||
ListNode * p2 = backhalf;
|
||||
ListNode * p2 = backhalf;
|
||||
while (p2 != nullptr) {
|
||||
if (p1->val != p2->val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
@@ -267,11 +291,11 @@ public:
|
||||
}
|
||||
p1 = p1->next;
|
||||
p2 = p2->next;
|
||||
}
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode->next = reverse(backhalf);
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
//找到中间的部分
|
||||
ListNode * searchmidnode (ListNode * head) {
|
||||
@@ -280,7 +304,7 @@ public:
|
||||
while (fast->next != nullptr && fast->next->next != nullptr) {
|
||||
fast = fast->next->next;
|
||||
slow = slow->next;
|
||||
}
|
||||
}
|
||||
return slow;
|
||||
}
|
||||
//翻转链表
|
||||
@@ -302,55 +326,55 @@ JS Code:
|
||||
|
||||
```javascript
|
||||
var isPalindrome = function (head) {
|
||||
if (head === null || head.next === null) {
|
||||
return true;
|
||||
}
|
||||
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
||||
let midnode = searchmidnode(head);
|
||||
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||
//这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||
let backhalf = reverse(midnode.next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
let p1 = head;
|
||||
let p2 = backhalf;
|
||||
while (p2 != null) {
|
||||
if (p1.val != p2.val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
midnode.next = reverse(backhalf);
|
||||
return false;
|
||||
if (head === null || head.next === null) {
|
||||
return true;
|
||||
}
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode.next = reverse(backhalf);
|
||||
return true;
|
||||
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
|
||||
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
|
||||
let midnode = searchmidnode(head);
|
||||
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
|
||||
//这里我们用的是midnode.next需要注意,因为我们找到的是中点,但是我们翻转的是后半部分
|
||||
let backhalf = reverse(midnode.next);
|
||||
//遍历两部分链表,判断值是否相等
|
||||
let p1 = head;
|
||||
let p2 = backhalf;
|
||||
while (p2 != null) {
|
||||
if (p1.val != p2.val) {
|
||||
//若要还原,记得这里也要reverse
|
||||
midnode.next = reverse(backhalf);
|
||||
return false;
|
||||
}
|
||||
p1 = p1.next;
|
||||
p2 = p2.next;
|
||||
}
|
||||
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
|
||||
//当然如果没有这一步也是可以AC,但是面试的时候题目要求可能会有这一条。
|
||||
midnode.next = reverse(backhalf);
|
||||
return true;
|
||||
};
|
||||
|
||||
//找到中点
|
||||
var searchmidnode = function (head) {
|
||||
let fast = head;
|
||||
let slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return slow;
|
||||
let fast = head;
|
||||
let slow = head;
|
||||
while (fast.next != null && fast.next.next != null) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
}
|
||||
return slow;
|
||||
};
|
||||
|
||||
//翻转链表
|
||||
var reverse = function (slow) {
|
||||
let low = null;
|
||||
let temp = null;
|
||||
while (slow != null) {
|
||||
temp = slow.next;
|
||||
slow.next = low;
|
||||
low = slow;
|
||||
slow = temp;
|
||||
}
|
||||
return low;
|
||||
let temp = null;
|
||||
while (slow != null) {
|
||||
temp = slow.next;
|
||||
slow.next = low;
|
||||
low = slow;
|
||||
slow = temp;
|
||||
}
|
||||
return low;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -457,3 +481,53 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func isPalindrome(head *ListNode) bool {
|
||||
if head == nil || head.Next == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
midNode := searchMidNode(head)
|
||||
backHalf := reverse(midNode.Next)
|
||||
|
||||
// 判断左右两边是否一样(回文)
|
||||
p1, p2 := head, backHalf
|
||||
for p2 != nil {
|
||||
if p1.Val != p2.Val {
|
||||
midNode.Next = reverse(backHalf)
|
||||
return false
|
||||
}
|
||||
p1 = p1.Next
|
||||
p2 = p2.Next
|
||||
}
|
||||
// 不破坏原来的数据
|
||||
midNode.Next = reverse(backHalf)
|
||||
return true
|
||||
}
|
||||
|
||||
// searchMidNode 求中间的节点
|
||||
func searchMidNode(head *ListNode) *ListNode {
|
||||
fast, slow := head, head
|
||||
for fast.Next != nil && fast.Next.Next != nil {
|
||||
fast = fast.Next.Next
|
||||
slow = slow.Next
|
||||
}
|
||||
return slow
|
||||
}
|
||||
|
||||
// reverse 反转链表
|
||||
func reverse(node *ListNode) *ListNode {
|
||||
var pre *ListNode
|
||||
for node != nil {
|
||||
nxt := node.Next
|
||||
node.Next = pre
|
||||
pre = node
|
||||
node = nxt
|
||||
}
|
||||
return pre
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -124,3 +124,22 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func hasCycle(head *ListNode) bool {
|
||||
if head == nil { return false }
|
||||
s, f := head, head
|
||||
for f != nil && f.Next != nil {
|
||||
s = s.Next
|
||||
f = f.Next.Next
|
||||
if s == f {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -297,3 +297,29 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func detectCycle(head *ListNode) *ListNode {
|
||||
if head == nil { return nil }
|
||||
s, f := head, head
|
||||
for f != nil && f.Next != nil {
|
||||
s = s.Next
|
||||
f = f.Next.Next
|
||||
// 快慢指针相遇
|
||||
if f == s {
|
||||
// 快指针从头开始一步一步走,也可以用一个新的指针
|
||||
f = head
|
||||
for f != s {
|
||||
f = f.Next
|
||||
s = s.Next
|
||||
}
|
||||
return f
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -257,3 +257,36 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func insertionSortList(head *ListNode) *ListNode {
|
||||
if head == nil || head.Next == nil { return head }
|
||||
root := &ListNode{
|
||||
Next: head,
|
||||
}
|
||||
cur, nxt := head, head.Next
|
||||
for nxt != nil {
|
||||
// 有序的不需要换位置
|
||||
if cur.Val <= nxt.Val {
|
||||
cur = cur.Next
|
||||
nxt = nxt.Next
|
||||
continue
|
||||
}
|
||||
temp := root
|
||||
for temp.Next.Val <= nxt.Val {
|
||||
temp = temp.Next
|
||||
}
|
||||
// 此时找到合适的位置
|
||||
cur.Next = nxt.Next
|
||||
nxt.Next = temp.Next
|
||||
temp.Next = nxt
|
||||
// 继续向下
|
||||
nxt = cur.Next
|
||||
}
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -164,6 +164,27 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func reverseList(head *ListNode) *ListNode {
|
||||
if head == nil || head.Next == nil { return head }
|
||||
cur := head
|
||||
var pre *ListNode
|
||||
for cur != nil {
|
||||
nxt := cur.Next
|
||||
cur.Next = pre
|
||||
pre = cur
|
||||
cur = nxt
|
||||
if nxt == nil {
|
||||
return pre
|
||||
}
|
||||
nxt = nxt.Next
|
||||
}
|
||||
return pre
|
||||
}
|
||||
```
|
||||
|
||||
上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。
|
||||
|
||||
**题目代码**
|
||||
@@ -217,19 +238,19 @@ JS Code:
|
||||
|
||||
```javascript
|
||||
var reverseList = function (head) {
|
||||
//结束条件
|
||||
if (!head || !head.next) {
|
||||
return head;
|
||||
}
|
||||
//保存最后一个节点
|
||||
let pro = reverseList(head.next);
|
||||
//将节点进行反转。我们可以这样理解 4.next.next = 4
|
||||
//4.next = 5
|
||||
//则 5.next = 4 则实现了反转
|
||||
head.next.next = head;
|
||||
//防止循环
|
||||
head.next = null;
|
||||
return pro;
|
||||
//结束条件
|
||||
if (!head || !head.next) {
|
||||
return head;
|
||||
}
|
||||
//保存最后一个节点
|
||||
let pro = reverseList(head.next);
|
||||
//将节点进行反转。我们可以这样理解 4.next.next = 4
|
||||
//4.next = 5
|
||||
//则 5.next = 4 则实现了反转
|
||||
head.next.next = head;
|
||||
//防止循环
|
||||
head.next = null;
|
||||
return pro;
|
||||
};
|
||||
```
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
### [328. 奇偶链表](https://leetcode-cn.com/problems/odd-even-linked-list/)
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
示例 2:
|
||||
|
||||
> 输入: 2->1->3->5->6->4->7->NULL
|
||||
> 输入: 2->1->3->5->6->4->7->NULL
|
||||
> 输出: 2->3->6->7->1->5->4->NULL
|
||||
|
||||
#### 题目解析
|
||||
@@ -54,7 +54,7 @@ class Solution {
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
}
|
||||
//链接
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
@@ -81,7 +81,7 @@ public:
|
||||
odd = odd->next;
|
||||
even->next = odd->next;
|
||||
even = even->next;
|
||||
}
|
||||
}
|
||||
//链接
|
||||
odd->next = evenHead;
|
||||
return head;
|
||||
@@ -98,15 +98,15 @@ var oddEvenList = function (head) {
|
||||
even = head.next,
|
||||
evenHead = even;
|
||||
while (odd.next && even.next) {
|
||||
//将偶数位合在一起,奇数位合在一起
|
||||
odd.next = even.next;
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
//链接
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
//将偶数位合在一起,奇数位合在一起
|
||||
odd.next = even.next;
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
//链接
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -155,3 +155,23 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func oddEvenList(head *ListNode) *ListNode {
|
||||
if head == nil || head.Next == nil {
|
||||
return head
|
||||
}
|
||||
odd, even := head, head.Next
|
||||
evenHead := even
|
||||
for odd.Next != nil && even.Next != nil {
|
||||
odd.Next = even.Next
|
||||
odd = odd.Next
|
||||
even.Next = odd.Next
|
||||
even = even.Next
|
||||
}
|
||||
odd.Next = evenHead
|
||||
return head
|
||||
}
|
||||
```
|
||||
|
@@ -194,3 +194,32 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func deleteDuplicates(head *ListNode) *ListNode {
|
||||
// 新建一个头结点,他的下一个节点才是开始
|
||||
root := &ListNode{
|
||||
Next: head,
|
||||
}
|
||||
pre, cur := root, head
|
||||
for cur != nil && cur.Next != nil {
|
||||
if cur.Val == cur.Next.Val {
|
||||
// 相等的话,cur就一直向后移动
|
||||
for cur != nil && cur.Next != nil && cur.Val == cur.Next.Val {
|
||||
cur = cur.Next
|
||||
}
|
||||
// 循环后移动到了最后一个相同的节点。
|
||||
cur = cur.Next
|
||||
pre.Next = cur
|
||||
} else {
|
||||
cur = cur.Next
|
||||
pre = pre.Next
|
||||
}
|
||||
}
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -187,3 +187,31 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func partition(head *ListNode, x int) *ListNode {
|
||||
big, small := &ListNode{}, &ListNode{}
|
||||
headBig, headSmall := big, small
|
||||
temp := head
|
||||
for temp != nil {
|
||||
// 分开存
|
||||
if temp.Val < x {
|
||||
small.Next = temp
|
||||
small = small.Next
|
||||
} else {
|
||||
big.Next = temp
|
||||
big = big.Next
|
||||
}
|
||||
temp = temp.Next
|
||||
}
|
||||
// 最后一个节点指向nil
|
||||
big.Next = nil
|
||||
// 存小数的链表和存大数的连起来
|
||||
small.Next = headBig.Next
|
||||
return headSmall.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -263,3 +263,48 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
GoCode:
|
||||
|
||||
```go
|
||||
func reverseBetween(head *ListNode, left int, right int) *ListNode {
|
||||
root := &ListNode{
|
||||
Next: head,
|
||||
}
|
||||
temp := root
|
||||
i := 0
|
||||
// left的前一个节点
|
||||
for ; i < left - 1; i++ {
|
||||
temp = temp.Next
|
||||
}
|
||||
leftNode := temp
|
||||
// right的后一个节点
|
||||
for ; i < right; i++ {
|
||||
temp = temp.Next
|
||||
}
|
||||
rightNode := temp.Next
|
||||
// 切断链表
|
||||
temp.Next = nil
|
||||
newhead := leftNode.Next
|
||||
leftNode.Next = nil
|
||||
|
||||
// 反转后将3段链表接上。
|
||||
leftNode.Next = reverse(newhead)
|
||||
newhead.Next = rightNode
|
||||
return root.Next
|
||||
}
|
||||
|
||||
func reverse(head *ListNode) *ListNode {
|
||||
var pre *ListNode
|
||||
cur := head
|
||||
for cur != nil {
|
||||
temp := cur
|
||||
cur = cur.Next
|
||||
temp.Next = pre
|
||||
pre = temp
|
||||
}
|
||||
return pre
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -146,3 +146,31 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
root := &ListNode{}
|
||||
node := root
|
||||
for l1 != nil && l2 != nil {
|
||||
if l1.Val < l2.Val {
|
||||
node.Next = l1
|
||||
l1 = l1.Next
|
||||
} else {
|
||||
node.Next = l2
|
||||
l2 = l2.Next
|
||||
}
|
||||
node = node.Next
|
||||
}
|
||||
// node接上l1或l2剩下的节点
|
||||
if l1 != nil {
|
||||
node.Next = l1
|
||||
} else {
|
||||
node.Next = l2
|
||||
}
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -265,6 +265,28 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func getIntersectionNode(headA, headB *ListNode) *ListNode {
|
||||
tempA, tempB := headA, headB
|
||||
for tempA != tempB {
|
||||
// 如果不为空就指针下移,为空就跳到另一链表的头部
|
||||
if tempA == nil {
|
||||
tempA = headB
|
||||
} else {
|
||||
tempA = tempA.Next
|
||||
}
|
||||
if tempB == nil {
|
||||
tempB = headA
|
||||
} else {
|
||||
tempB = tempB.Next
|
||||
}
|
||||
}
|
||||
return tempA
|
||||
}
|
||||
```
|
||||
|
||||
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
|
||||
|
||||
<br/>
|
||||
|
@@ -163,3 +163,23 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func getKthFromEnd(head *ListNode, k int) *ListNode {
|
||||
if head == nil { return head }
|
||||
pro, after := head, head
|
||||
//先移动绿指针到指定位置
|
||||
for i := 0; i < k - 1; i++ {
|
||||
pro = pro.Next
|
||||
}
|
||||
for pro.Next != nil {
|
||||
pro = pro.Next
|
||||
after = after.Next
|
||||
}
|
||||
return after
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -135,3 +135,19 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func middleNode(head *ListNode) *ListNode {
|
||||
// 快慢指针
|
||||
fast, slow := head, head
|
||||
for fast != nil && fast.Next != nil {
|
||||
fast = fast.Next.Next
|
||||
slow = slow.Next
|
||||
}
|
||||
return slow
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -267,3 +267,42 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
|
||||
root := &ListNode{}
|
||||
temp := root
|
||||
// 用来保存进位值,初始化为0
|
||||
mod := 0
|
||||
for (l1 != nil || l2 != nil) {
|
||||
l1num := 0
|
||||
if l1 != nil { l1num = l1.Val }
|
||||
l2num := 0
|
||||
if l2 != nil { l2num = l2.Val }
|
||||
// 将链表的值和进位值相加,得到为返回链表的值
|
||||
sum := l1num + l2num + mod
|
||||
// 更新进位值,例18/10=1,9/10=0
|
||||
mod = sum / 10
|
||||
// 新节点保存的值,18%8=2,则添加2
|
||||
sum = sum % 10
|
||||
newNode := &ListNode{
|
||||
Val: sum,
|
||||
}
|
||||
temp.Next = newNode
|
||||
temp = temp.Next
|
||||
if l1 != nil { l1 = l1.Next }
|
||||
if l2 != nil { l2 = l2.Next }
|
||||
}
|
||||
if mod != 0 {
|
||||
newNode := &ListNode{
|
||||
Val: mod,
|
||||
}
|
||||
temp.Next = newNode
|
||||
}
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user