题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param pHead1 ListNode类 * @param pHead2 ListNode类 * @return ListNode类 */ func Merge(pHead1 *ListNode, pHead2 *ListNode) *ListNode { var head *ListNode var m *ListNode for p,q := pHead1,pHead2;p != nil || q != nil;{ if q == nil || (p != nil && p.Val < q.Val) { if head == nil{ head = p m = p }else { m.Next = p m = p } p = p.Next m.Next = nil continue } if p == nil || (q != nil && q.Val <= p.Val){ if head == nil{ head = q m = q }else{ m.Next = q m = q } q = q.Next m.Next = nil } } return head // write code here }