-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_24_Swap_Nodes_in_PairsCS.cs
126 lines (99 loc) · 4.22 KB
/
leetcode_24_Swap_Nodes_in_PairsCS.cs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Linq;
/*
* https://leetcode.com/problems/swap-nodes-in-pairs/
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
*/
using System.Text;
using System.Threading.Tasks;
namespace leetcode_24_Swap_Nodes_in_PairsCS
{
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
class Program
{
public static ListNode create_linked_node(int[] arr_collection , int first_element , int last_element)
{
// create the list node and allocate the memory to the list node
ListNode root = null;
if (first_element <= last_element)
{
// iterating all the values in the arr collection until the collection gets over
// recursively create a new node
root = new ListNode(arr_collection[first_element]);
// assign the memory to the newly created node
root.next = create_linked_node(arr_collection, first_element + 1, last_element);
}
return root;
}
public static ListNode build_linked_list(int[] arr_collection)
{
int front = arr_collection.GetLowerBound(0);
int last = arr_collection.GetUpperBound(0);
// get the front_index and the last_index value
ListNode root = create_linked_node(arr_collection, front, last);
// function to create the linked list structure based on the input arr-collection
return root;
}
public static void visit_linked_list(ListNode root)
{
// visit all the linked nodes present in the linked list iteratively
// if the node is null , return
// if the node contains a single node , print the node
ListNode temp_root = root;
// donot disturb the root node
while (temp_root != null)
{
// if the node is not null , print the data pointed by the list node
Console.Write(temp_root.val + " ");
temp_root = temp_root.next;
}
}
public static ListNode SwapPairs(ListNode head)
{
if (head != null && head.next != null)
{
// atleast two nodes to swap
// 1--> 2-->3-->4
ListNode newhead = head.next;
// 1(head)--> 2(newhead)-->3-->4
ListNode remaining = newhead.next;
// 1(head)--> 2(newhead)-->3(remaining)-->4
// swap the links
newhead.next = head;
// 2(newhead)<-->1(head) 3(remaining)-->4
head.next = SwapPairs(remaining);
// 2(newhead)-->1(head)--->[(3(remaining)-->4) = unsolved problem]
return newhead;
}
return head;
}
static void Main(string[] args)
{
int[] arr_collection = new int[] { 1 , 2 , 3 , 4 };
// given integer array containing the values of the node.
ListNode root = null;
// root element of the node
root = build_linked_list(arr_collection);
// build the linked list node structure with the given data list
Console.WriteLine("the elements in the linked list are : ");
visit_linked_list(root);
// browse all the elements of the linked list iteratively
Console.WriteLine();
root = SwapPairs(root);
// call the swap function to swap the alternate nodes recursively.
Console.WriteLine("the elements in the linked list after the swap are : ");
visit_linked_list(root);
// browse all the elements of the linked list iteratively
Console.ReadLine();
}
}
}