mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
@ -114,24 +114,28 @@ class Solution {
|
|||||||
```
|
```
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
* type ListNode struct {
|
||||||
|
* Val int
|
||||||
|
* Next *ListNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
||||||
result:=&ListNode{}
|
dummyHead := &ListNode{}
|
||||||
result.Next=head
|
dummyHead.Next = head
|
||||||
var pre *ListNode
|
cur := head
|
||||||
cur:=result
|
prev := dummyHead
|
||||||
|
i := 1
|
||||||
i:=1
|
for cur != nil {
|
||||||
for head!=nil{
|
cur = cur.Next
|
||||||
if i>=n{
|
if i > n {
|
||||||
pre=cur
|
prev = prev.Next
|
||||||
cur=cur.Next
|
|
||||||
}
|
}
|
||||||
head=head.Next
|
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
pre.Next=pre.Next.Next
|
prev.Next = prev.Next.Next
|
||||||
return result.Next
|
return dummyHead.Next
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -153,6 +153,19 @@ func swapPairs(head *ListNode) *ListNode {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 递归版本
|
||||||
|
func swapPairs(head *ListNode) *ListNode {
|
||||||
|
if head == nil || head.Next == nil {
|
||||||
|
return head
|
||||||
|
}
|
||||||
|
next := head.Next
|
||||||
|
head.Next = swapPairs(next.Next)
|
||||||
|
next.Next = head
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
```javascript
|
```javascript
|
||||||
var swapPairs = function (head) {
|
var swapPairs = function (head) {
|
||||||
|
Reference in New Issue
Block a user