1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func removeNodes(head *ListNode) *ListNode { revertHead := revertLinkNode(head) node := revertHead p := &ListNode{ Val: -1, Next: nil, } pre := p for node != nil { if pre.Val <= node.Val { pre.Next = node node = node.Next pre = pre.Next pre.Next = nil } else { node = node.Next } } return revertLinkNode(p.Next) }
func revertLinkNode(head *ListNode) *ListNode { p := &ListNode{ Val: -1, Next: nil, } node := head for node != nil { tmp := p.Next p.Next = node node = node.Next p.Next.Next = tmp } return p.Next }
|