Query related to linked list

found this code here

func (l *LinkedList) Insert(val int) {
	n := Node{}
	n.value = val
	if l.len == 0 {
		l.head = &n
		l.len++
		return
	}
	ptr := l.head
	for i := 0; i < l.len; i++ {
		if ptr.next == nil {
			ptr.next = &n
			l.len++
			return
		}
		ptr = ptr.next
	}
}

I wanna know that (l *LinkedList) conveys here

It means that Insert is a method of *LinkedList. l is the “receiver”. A Tour of Go

2 Likes

thanks man for helping

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
    m := &ListNode{}
    for c := m; l1 != nil || l2 != nil; c = c.Next {
        switch {
        case l1 == nil: c.Next, l2 = l2, l2.Next
        case l2 == nil: c.Next, l1 = l1, l1.Next
        case l2.Val > l1.Val: c.Next, l1 = l1, l1.Next
        default: c.Next, l2 = l2, l2.Next
        }
    }
    return m.Next
}

can you please explain l1=l1 and l2=l2 mean

Those are multiple assignments in each case. For example,
c.Next, l2 = l2, l2.Next
assigns l2 to c.Next, and l2.Next to l2.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.