-
Notifications
You must be signed in to change notification settings - Fork 6
/
SwapNodesInPairs.java
39 lines (29 loc) · 1.05 KB
/
SwapNodesInPairs.java
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
package com.shivaprasad.january.day18;
//Note: For Linked List problems, Just focus on method code because creating Linked List for every problem is a hectic task,
//and we don't need to worry about it.
//Problem Link: https://leetcode.com/problems/swap-nodes-in-pairs/
public class SwapNodesInPairs {
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode swapPairs(ListNode head) {
return pairsSwap(head);
}
public ListNode pairsSwap(ListNode head)
{
if(head == null || head.next == null)
return head;
ListNode first = head;
ListNode second = head.next;
ListNode tempList = pairsSwap(head.next.next);
first.next = tempList;
second.next = first;
return second;
//T.C: O(n)
//S.C: O(1) but auxiliary space takes by recursion call stack will be O(n/2) nothing but O(n)
}
}