Write the java code that will convert the given Before links to the After links with comments.
For example:
Before:
+----+----+ +----+----+
list ----> | 1 | +----> | 2 | / |
+----+----+ +----+----+
After:
+----+----+ +----+----+ +----+----+
list ----> | 1 | +----> | 2 | +----> | 3 | / |
+----+----+ +----+----+ +----+----+
Code: (example answer)
list.next.next = new ListNode(3, null); // 2 -> 3
(1)
Before:
+----+----+ +----+----+
list ----> | 1 | +----> | 2 | / |
+----+----+ +----+----+
After:
+----+----+ +----+----+ +----+----+
list ----> | 3 | +----> | 1 | +----> | 2 | / |
+----+----+ +----+----+ +----+----+
Code:
-----------------------------------------------------------------
(2)
Before:
+----+----+ +----+----+
list ----> | 1 | +----> | 2 | / |
+----+----+ +----+----+
+----+----+ +----+----+
temp ----> | 3 | +----> | 4 | / |
+----+----+ +----+----+
After:
+----+----+ +----+----+ +----+----+ +----+----+
list ----> | 1 | +----> | 3 | +----> | 4 | +- --> | 2 | /
+----+----+ +----+----+ +----+----+ +----+----+
Code:
-----------------------------------------------------------------
(3)
Before
+----+----+ +----+----+ +----+----+
list ----> | 1 | +----> | 2 | +----> | 3 | / |
+----+----+ +----+----+ +----+----+
After:
+----+----+
list ----> | 2 | / |
+----+----+
+----+----+ +----+----+
List2 ---> | 1 | +----> | 3 | / |
+----+----+ +----+----+
Code:
-----------------------------------------------------------------
(4)
Before
+----+----+ +----+----+ +----+----+
list ----> | 5 | +----> | 4 | +----> | 3 | / |
+----+----+ +----+----+ +----+----+
After:
+----+----+ +----+----+ +----+----+
list ----> | 3 | +----> | 4 | +----> | 5 | / |
+----+----+ +----+----+ +----+----+
Code:
-----------------------------------------------------------------
(5)
Before:
+----+----+
list ----> | 1 | / |
+----+----+
+----+----+ +----+----+ +----+----+
list2 ---> | 2 | +----> | 3 | +----> | 4 | / |
+----+----+ +----+----+ +----+----+
After:
+----+----+ +----+----+ +----+----+
list2 ---> | 4 | +----> | 1 | +----> | 2 | / |
+----+----+ +----+----+ +----+----+
+----+----+
List2 ---> | 3 | / |
+----+----+
Code:
1)
list = new ListNode(3, list);
2)
temp->next->next = list->next;
list->next = temp;
3)
ListNode* list2 = list;
list = list->next;
list2->next = list2->next->next;
list->next = NULL;
4)
ListNode* t = list->next->next;
t->next = list->next;
list->next->next = list;
list->next->next->next = NULL;
list = t
5)
list->next->next->next = list;
list = list->next->next;
ListNode* list2 = list->next->next;
list->next->next = NULL;
Get Answers For Free
Most questions answered within 1 hours.