Skip to content

Latest commit

 

History

History
76 lines (64 loc) · 1.22 KB

swapNodesInLinkedList.md

File metadata and controls

76 lines (64 loc) · 1.22 KB

Problem

You are given the head of a linked list, and an integer k.

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

Examples

Example 1
Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]

Example 2
Input: head = [1], k = 1
Output: [1]

Solution

Golang

type ListNode struct {
    Val int
    Next *ListNode
}
 
func max(a,b int) int{
    if a>b{
        return a
    }
    return b
}

func swapNodes(head *ListNode, k int) *ListNode {
    n := findLen(head)
    tmp := head
    first,sec := k,n-k+1
    // corner case
    if first == sec{
        return head
    }
    var firstNode,secNode *ListNode
    for i:=1;i<=max(first,sec);i++{
        if i==first{
            firstNode=tmp
        }else if i==sec{
            secNode=tmp
        }
        tmp = tmp.Next
    }
    // swap
    firstNode.Val,secNode.Val = secNode.Val, firstNode.Val
    return head
}

func findLen(head *ListNode) int {
    n := 0
    tmp := head
    for tmp != nil{
        n++
        tmp = tmp.Next
    }
    return n
}

Python