Skip to content

Instantly share code, notes, and snippets.

@scaffeinate
Created January 11, 2019 05:05
Show Gist options
  • Save scaffeinate/0bfcd768d770a186627db2f18894a060 to your computer and use it in GitHub Desktop.
Save scaffeinate/0bfcd768d770a186627db2f18894a060 to your computer and use it in GitHub Desktop.
public static SinglyLinkedListNode removeNodes(SinglyLinkedListNode listHead, int x) {
SinglyLinkedListNode head = null, tail = null;
while (listHead != null) {
if (listHead.data <= x) {
SinglyLinkedListNode node = new SinglyLinkedListNode();
node.data = listHead.data;
if(head == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
}
listHead = listHead.next;
}
return head;
}
@xkolarik
Copy link

xkolarik commented Aug 12, 2022

`public static SinglyLinkedListNode removeNodes(SinglyLinkedListNode listHead, int x) {
SinglyLinkedListNode head = null, tail = null;
while (listHead != null) {
if (listHead.data <= x) {
SinglyLinkedListNode node = new SinglyLinkedListNode(x);
node.data = listHead.data;
if(head == null) {
head = tail = node;
} else {
tail.next = node;
tail = node;
}
}

    listHead = listHead.next;
}

return head;

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment