Adds the first element of linked list list_1 as a last element of linked list list_2 and removes it from list_1
For example, if list_2 = 5, 6, 7, 8
and list_1 = 9, 10, 11, 12
, then:Addtolist2(list_2, list_1)
will change list_2 to 5, 6, 7, 8, 9
and change list_1
to 10, 11, 12
. Note that you should not create new lists in this method.
public class ListNode
int data;
ListNode next;
public class List
ListNode header;
This what I tried:
public static void Addtolist2(list_2, List_1)
ListNode p = l2.header.next;
while(p != null)
p = p.next;
p = l1.header.next;
ListNode q = l1.header.next;
if (l1.header.next.next != null)
q = l1.header.next.next;
java
add a comment |
For example, if list_2 = 5, 6, 7, 8
and list_1 = 9, 10, 11, 12
, then:Addtolist2(list_2, list_1)
will change list_2 to 5, 6, 7, 8, 9
and change list_1
to 10, 11, 12
. Note that you should not create new lists in this method.
public class ListNode
int data;
ListNode next;
public class List
ListNode header;
This what I tried:
public static void Addtolist2(list_2, List_1)
ListNode p = l2.header.next;
while(p != null)
p = p.next;
p = l1.header.next;
ListNode q = l1.header.next;
if (l1.header.next.next != null)
q = l1.header.next.next;
java
add a comment |
For example, if list_2 = 5, 6, 7, 8
and list_1 = 9, 10, 11, 12
, then:Addtolist2(list_2, list_1)
will change list_2 to 5, 6, 7, 8, 9
and change list_1
to 10, 11, 12
. Note that you should not create new lists in this method.
public class ListNode
int data;
ListNode next;
public class List
ListNode header;
This what I tried:
public static void Addtolist2(list_2, List_1)
ListNode p = l2.header.next;
while(p != null)
p = p.next;
p = l1.header.next;
ListNode q = l1.header.next;
if (l1.header.next.next != null)
q = l1.header.next.next;
java
For example, if list_2 = 5, 6, 7, 8
and list_1 = 9, 10, 11, 12
, then:Addtolist2(list_2, list_1)
will change list_2 to 5, 6, 7, 8, 9
and change list_1
to 10, 11, 12
. Note that you should not create new lists in this method.
public class ListNode
int data;
ListNode next;
public class List
ListNode header;
This what I tried:
public static void Addtolist2(list_2, List_1)
ListNode p = l2.header.next;
while(p != null)
p = p.next;
p = l1.header.next;
ListNode q = l1.header.next;
if (l1.header.next.next != null)
q = l1.header.next.next;
java
java
edited Nov 14 '18 at 14:45
deHaar
2,41441628
2,41441628
asked Nov 14 '18 at 14:38
user10625272
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Maybe try something like this (you might have to change a few things)
public static void Addtolist2(List list2, List list1)
// Find last node in list2
ListNode lastEl = list2.header;
while(lastEl != null)
lastEl = lastEl.next;
// Take the first element of list1
ListNode firstEl = list1.header;
// Add new element to list2 in last
ListNode newEl = new ListNode();
newEl.data = firstEl.data;
lastEl = newEl;
// Remove first element of list1 (replace with second)
list1.header = firstEl.next;
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
add a comment |
List<Integer> lst1 = new ArrayList<>(Arrays.asList(5, 6, 7, 8));
List<Integer> lst2 = new ArrayList<>(Arrays.asList(9, 10, 11, 12));
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
Integer moved = lst2.get(0);
lst2.remove(0);
lst1.add(moved);
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
add a comment |
public class MainClass
public static void main(String args)
LinkedList<Integer> list1 = new LinkedList<Integer>();
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
LinkedList<Integer> list2 = new LinkedList<Integer>();
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(12);
Integer element = list2.getFirst();
list1.addLast(element);
list2.removeFirst();
System.out.println("First List:");
System.out.print(list1);
System.out.println();
System.out.println("Second List: ");
System.out.print(list2);
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302707%2fadds-the-first-element-of-linked-list-list-1-as-a-last-element-of-linked-list-li%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe try something like this (you might have to change a few things)
public static void Addtolist2(List list2, List list1)
// Find last node in list2
ListNode lastEl = list2.header;
while(lastEl != null)
lastEl = lastEl.next;
// Take the first element of list1
ListNode firstEl = list1.header;
// Add new element to list2 in last
ListNode newEl = new ListNode();
newEl.data = firstEl.data;
lastEl = newEl;
// Remove first element of list1 (replace with second)
list1.header = firstEl.next;
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
add a comment |
Maybe try something like this (you might have to change a few things)
public static void Addtolist2(List list2, List list1)
// Find last node in list2
ListNode lastEl = list2.header;
while(lastEl != null)
lastEl = lastEl.next;
// Take the first element of list1
ListNode firstEl = list1.header;
// Add new element to list2 in last
ListNode newEl = new ListNode();
newEl.data = firstEl.data;
lastEl = newEl;
// Remove first element of list1 (replace with second)
list1.header = firstEl.next;
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
add a comment |
Maybe try something like this (you might have to change a few things)
public static void Addtolist2(List list2, List list1)
// Find last node in list2
ListNode lastEl = list2.header;
while(lastEl != null)
lastEl = lastEl.next;
// Take the first element of list1
ListNode firstEl = list1.header;
// Add new element to list2 in last
ListNode newEl = new ListNode();
newEl.data = firstEl.data;
lastEl = newEl;
// Remove first element of list1 (replace with second)
list1.header = firstEl.next;
Maybe try something like this (you might have to change a few things)
public static void Addtolist2(List list2, List list1)
// Find last node in list2
ListNode lastEl = list2.header;
while(lastEl != null)
lastEl = lastEl.next;
// Take the first element of list1
ListNode firstEl = list1.header;
// Add new element to list2 in last
ListNode newEl = new ListNode();
newEl.data = firstEl.data;
lastEl = newEl;
// Remove first element of list1 (replace with second)
list1.header = firstEl.next;
answered Nov 14 '18 at 16:02
NeskuNesku
4151311
4151311
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
add a comment |
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Thank you so much
– user10625272
Nov 14 '18 at 22:23
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted.
– Nesku
Nov 15 '18 at 7:30
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
This really helped me a lot , I can't thank you enough really god bless you and I added this statement ==> newEl.next = null;
– user10625272
Nov 16 '18 at 14:18
add a comment |
List<Integer> lst1 = new ArrayList<>(Arrays.asList(5, 6, 7, 8));
List<Integer> lst2 = new ArrayList<>(Arrays.asList(9, 10, 11, 12));
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
Integer moved = lst2.get(0);
lst2.remove(0);
lst1.add(moved);
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
add a comment |
List<Integer> lst1 = new ArrayList<>(Arrays.asList(5, 6, 7, 8));
List<Integer> lst2 = new ArrayList<>(Arrays.asList(9, 10, 11, 12));
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
Integer moved = lst2.get(0);
lst2.remove(0);
lst1.add(moved);
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
add a comment |
List<Integer> lst1 = new ArrayList<>(Arrays.asList(5, 6, 7, 8));
List<Integer> lst2 = new ArrayList<>(Arrays.asList(9, 10, 11, 12));
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
Integer moved = lst2.get(0);
lst2.remove(0);
lst1.add(moved);
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
List<Integer> lst1 = new ArrayList<>(Arrays.asList(5, 6, 7, 8));
List<Integer> lst2 = new ArrayList<>(Arrays.asList(9, 10, 11, 12));
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
Integer moved = lst2.get(0);
lst2.remove(0);
lst1.add(moved);
System.out.println("lst1 = " + lst1 + " lst2 = " + lst2);
answered Nov 14 '18 at 14:48
IlyaIlya
21719
21719
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
add a comment |
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
What if i wanna write it as a method from these two classes ?
– user10625272
Nov 14 '18 at 15:11
add a comment |
public class MainClass
public static void main(String args)
LinkedList<Integer> list1 = new LinkedList<Integer>();
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
LinkedList<Integer> list2 = new LinkedList<Integer>();
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(12);
Integer element = list2.getFirst();
list1.addLast(element);
list2.removeFirst();
System.out.println("First List:");
System.out.print(list1);
System.out.println();
System.out.println("Second List: ");
System.out.print(list2);
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
add a comment |
public class MainClass
public static void main(String args)
LinkedList<Integer> list1 = new LinkedList<Integer>();
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
LinkedList<Integer> list2 = new LinkedList<Integer>();
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(12);
Integer element = list2.getFirst();
list1.addLast(element);
list2.removeFirst();
System.out.println("First List:");
System.out.print(list1);
System.out.println();
System.out.println("Second List: ");
System.out.print(list2);
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
add a comment |
public class MainClass
public static void main(String args)
LinkedList<Integer> list1 = new LinkedList<Integer>();
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
LinkedList<Integer> list2 = new LinkedList<Integer>();
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(12);
Integer element = list2.getFirst();
list1.addLast(element);
list2.removeFirst();
System.out.println("First List:");
System.out.print(list1);
System.out.println();
System.out.println("Second List: ");
System.out.print(list2);
public class MainClass
public static void main(String args)
LinkedList<Integer> list1 = new LinkedList<Integer>();
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
LinkedList<Integer> list2 = new LinkedList<Integer>();
list2.add(9);
list2.add(10);
list2.add(11);
list2.add(12);
Integer element = list2.getFirst();
list1.addLast(element);
list2.removeFirst();
System.out.println("First List:");
System.out.print(list1);
System.out.println();
System.out.println("Second List: ");
System.out.print(list2);
edited Nov 14 '18 at 15:03
answered Nov 14 '18 at 14:55
PulszarPulszar
43426
43426
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
add a comment |
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Thank you.. but i am trying to write a method from these two classes above :)
– user10625272
Nov 14 '18 at 15:10
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
Ok if you want to make your own implementation of the idea for linked list you can see which methods you will need. As you can see from above you need 1. Add element on a certain place (index), 2. remove element on certain place (index) and then you can easily make the method for removing and adding elements as I show you. Just split your task on smaller ones. For the implementation of a List and ListNode I can not help you right now. Maybe someone can give you idea or you can search on internet. Good luck!
– Pulszar
Nov 14 '18 at 15:26
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
okay.. Thanks alot 4 replying
– user10625272
Nov 14 '18 at 15:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302707%2fadds-the-first-element-of-linked-list-list-1-as-a-last-element-of-linked-list-li%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown